ChangePropertyAction不会影响FontWeight

时间:2015-11-20 14:38:30

标签: c# xaml windows-10 uwp textblock

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

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

我的自定义类有一个属性IsOpen。如果是,则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为真,则文本为绿色。但是,FontWeight未设置,文本看起来从不粗体。

奇怪的是,交换两个ChangePropertyAction会导致两个操作都没有被应用。

FontWeight静态更改TextBlock按预期工作。 FontWeight="Light"TextBlock未设置 时,也会出现此问题。

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

1 个答案:

答案 0 :(得分:2)

如果查看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>