如何在ViewModel中设置TextBlock的FontSize属性



我们制作了一个带有Label和嵌套TextBlock(用于文本包装)的WPF UserControl。因为我们需要在拖放之后复制UserControl,所以我们不能在UserControl XAML中使用名称。因此,我们需要从XAML中删除所有名称,并使用ViewModel。但是对于嵌套TextBlock的一些DependencyProperty,我们遇到了问题。当我们尝试按示例设置FonzSize属性时,它不会做出反应。同时,我们发现,如果我们用一个新名称定义一个属性,比如"ControlFontSize",它是有效的。但如果可能的话,我们想使用属性名称"FontSize"。有办法解决这个问题吗?

这是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>

这是UserControl的CS文件中的属性,它获取并设置ViewModel属性:

public new double FontSize
{
    get { return viewmodel.FontSize; }
    set { viewmodel.FontSize = value; }
}

这就是ViewModel属性本身:

public double FontSize
{
    get { return fontSize; }
    set
    {
        fontSize = value;
        RaisePropertyChanged("FontSize");
    }
}

我们自己已经发现了。您可以将TextBlock的FontSize属性绑定到它的祖先——在本例中是UserControl本身。

在UserControl的XAML中:

        <TextBlock Text="{Binding Path=Text, Mode=TwoWay, NotifyOnSourceUpdated=True, UpdateSourceTrigger=PropertyChanged}"
                   FontSize="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type UserControl}}, Path=FontSize}"

然后在测试窗口中,您可以像往常一样使用UserControl的FontSize属性。不需要具有新名称或DependencyProperty的属性。

最新更新