We have made a WPF UserControl with a Label and a nested TextBlock (for textwrapping). Because we need to copy the UserControl after Drag-and-Drop we can't use names in the UserControl-XAML. So we needed to remove all Names from the XAML and use a ViewModel. But with some DependencyProperties of the nested TextBlock we have problems. When we try to set the FonzSize-property per example, it doesn't react. Meanwhile we have found out, that it works, if we define a property with a new Name like "ControlFontSize". But if it's possible we want to use the propertyname "FontSize". Is there a way to handle this?
This is the UserControl-XAML:
<UserControl x:Class="WpfDesignerControlLibrary.ControlLabel"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:WpfDesignerControlLibrary"
mc:Ignorable="d"
d:DesignHeight="30"
d:DesignWidth="150">
<UserControl.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Resources.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</UserControl.Resources>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Border Style="{StaticResource usercontrolBorderStyle}"
BorderBrush="{Binding Path=BorderBrush, Mode=TwoWay, NotifyOnSourceUpdated=True, UpdateSourceTrigger=PropertyChanged}"
BorderThickness="{Binding Path=BorderThickness, Mode=TwoWay, NotifyOnSourceUpdated=True, UpdateSourceTrigger=PropertyChanged}">
<Label Margin="2"
HorizontalContentAlignment="{Binding Path=ControlHorizontalContentAlignment, Mode=TwoWay, NotifyOnSourceUpdated=True, UpdateSourceTrigger=PropertyChanged}"
VerticalContentAlignment="{Binding Path=ControlVerticalContentAlignment, Mode=TwoWay, NotifyOnSourceUpdated=True, UpdateSourceTrigger=PropertyChanged}">
<TextBlock Text="{Binding Path=Text, Mode=TwoWay, NotifyOnSourceUpdated=True, UpdateSourceTrigger=PropertyChanged}"
FontSize="{Binding Path=ControlFontSize, Mode=TwoWay, NotifyOnSourceUpdated=True, UpdateSourceTrigger=PropertyChanged}"
TextAlignment="{Binding Path=ControlTextAlignment, Mode=TwoWay, NotifyOnSourceUpdated=True, UpdateSourceTrigger=PropertyChanged}"
FontWeight="{Binding Path=FontWeight, Mode=TwoWay, NotifyOnSourceUpdated=True, UpdateSourceTrigger=PropertyChanged}"
FontStyle="{Binding Path=FontStyle, Mode=TwoWay, NotifyOnSourceUpdated=True, UpdateSourceTrigger=PropertyChanged}"
TextDecorations="{Binding Path=TextDecorations, Mode=TwoWay, NotifyOnSourceUpdated=True, UpdateSourceTrigger=PropertyChanged}"
TextWrapping="Wrap"/>
</Label>
</Border>
</Grid>
</UserControl>
That's the property in the CS-File of the UserControl, which gets and sets the ViewModel-property:
public new double FontSize
{
get { return viewmodel.FontSize; }
set { viewmodel.FontSize = value; }
}
And this is the ViewModel-Property itself:
public double FontSize
{
get { return fontSize; }
set
{
fontSize = value;
RaisePropertyChanged("FontSize");
}
}
We have found it out ourselves. You can bind the FontSize-property of the TextBlock to it's ancestor - in this case the UserControl itself.
In XAML of the UserControl:
<TextBlock Text="{Binding Path=Text, Mode=TwoWay, NotifyOnSourceUpdated=True, UpdateSourceTrigger=PropertyChanged}"
FontSize="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type UserControl}}, Path=FontSize}"
And in a testwindow You can then use the FontSize-property of the UserControl as usual. No need for a property with a new Name or DependencyProperty.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.