ChangePropertyAction does not affect FontWeight

本文关键字:FontWeight affect not does ChangePropertyAction | 更新日期: 2023-09-27 18:22:50

在我的通用Windows平台应用程序中,我有一个TextBlock,它的DataContext是一个自定义类:

<TextBlock Text="{Binding OpeningHourDescription}" FontWeight="Light">
    ...
</TextBlock>

我的自定义类具有属性IsOpen。如果为true,则TextBlock应更改其颜色和字体重量。我试图通过在TextBlock元素中替换以下XAML代码来定义它:

<Interactivity:Interaction.Behaviors>
    <Core:DataTriggerBehavior Binding="{Binding IsOpen}" Value="true">
        <Core:ChangePropertyAction PropertyName="Foreground" >
            <Core:ChangePropertyAction.Value>
                <Color>Lime</Color>
            </Core:ChangePropertyAction.Value>
        </Core:ChangePropertyAction>
        <Core:ChangePropertyAction PropertyName="FontWeight">
            <Core:ChangePropertyAction.Value>
                <FontWeight>ExtraBold</FontWeight>
            </Core:ChangePropertyAction.Value>
        </Core:ChangePropertyAction>
    </Core:DataTriggerBehavior>
</Interactivity:Interaction.Behaviors>

行为本身正在起作用:如果IsOpen为true,则文本为绿色。但是,没有设置FontWeight,文本看起来从不加粗。

奇怪的是,交换两个ChangePropertyAction会导致两个操作都不适用。

TextBlock上静态更改FontWeight可以按预期工作。当FontWeight="Light"TextBlock上设置为而不是时,也会出现此问题。

我做错了什么?如何使用ChangePropertyAction修改FontWeight

ChangePropertyAction does not affect FontWeight

如果查看Windows API,您会发现TextBlock.FontWeight属性的类型为FontWeight,这是一个包含Int16的结构。

public struct FontWeight
{
    public System.UInt16 Weight;
}

如果在控件上设置XAML属性,它将设法将字符串转换为FontWeights静态属性。

public sealed class FontWeights : IFontWeights
{
    public static FontWeight Black { get; }
    public static FontWeight Bold { get; }
    public static FontWeight ExtraBlack { get; }
    public static FontWeight ExtraBold { get; }
    public static FontWeight ExtraLight { get; }
    public static FontWeight Light { get; }
    public static FontWeight Medium { get; }
    public static FontWeight Normal { get; }
    public static FontWeight SemiBold { get; }
    public static FontWeight SemiLight { get; }
    public static FontWeight Thin { get; }
}

然而,在触发器中,您声明输入的字符串实际上是FontWeight结构(而不是FontWeights属性),因此系统在尝试转换字符串时出错。

<Core:ChangePropertyAction.Value>
     <FontWeight>ExtraBold</FontWeight>
</Core:ChangePropertyAction.Value>

解决方案:您可以使用GoToStateAction和VisualStates来解决问题。

<TextBlock x:Name="MyTextBlock"  Text="{Binding OpeningHourDescription}" FontWeight="Light">
    <interactivity:Interaction.Behaviors>
        <core:DataTriggerBehavior Binding="{Binding IsOpen}" Value="true">
            <core:GoToStateAction StateName="Open" >
            </core:GoToStateAction>
        </core:DataTriggerBehavior>
    </interactivity:Interaction.Behaviors>
</TextBlock>

并将正确的状态添加到容器控件中。

<VisualStateManager.VisualStateGroups>
    <VisualStateGroup x:Name="VisualStateGroup">
        <VisualState x:Name="Open">
            <VisualState.Setters>
                <Setter Target="MyTextBlock.Foreground" Value="Lime" />
                <Setter Target="MyTextBlock.FontWeight" Value="Bold" />
            </VisualState.Setters>
        </VisualState>
        ...
    </VisualStateGroup>
</VisualStateManager.VisualStateGroups>