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

How to set the FontSize property of a TextBlock in a ViewModel

我们制作了一个带有 Label 和嵌套 TextBlock(用于文本换行)的 WPF UserControl。 因为我们需要在拖放后复制 UserControl,所以我们不能在 UserControl-XAML 中使用名称。 所以我们需要从 XAML 中删除所有名称并使用 ViewModel。 但是对于嵌套 TextBlock 的某些 DependencyProperties,我们遇到了问题。 当我们尝试为每个示例设置 FonzSize-属性 时,它没有反应。 同时我们发现,如果我们用 "ControlFontSize" 这样的新名称定义 属性,它就可以工作。 但如果可能的话,我们希望使用 属性 名称 "FontSize"。 有办法处理吗?

这是用户控件-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 的 属性。