In my Universal Windows Platform app, I have a TextBlock
whose DataContext
is a custom class:
<TextBlock Text="{Binding OpeningHourDescription}" FontWeight="Light">
...
</TextBlock>
My custom class has a property IsOpen
. If it is true, the TextBlock
should change its color and font weight. I tried to define this by replacing the following XAML code within the TextBlock
element:
<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>
The behavior itself is working: If IsOpen
is true, the text is green. However, the FontWeight
is not being set, the text never looks bold.
Curiously, swapping both ChangePropertyAction
results in both actions not being applied.
Changing the FontWeight
statically on the TextBlock
works as expected. The issue also occurs when FontWeight="Light"
is not set on the TextBlock
.
What am I doing wrong? How can I modify the FontWeight
with a ChangePropertyAction
?
If you look into the Windows API, you'll see that a TextBlock.FontWeight property is of type FontWeight
, which is a struct containing an Int16.
public struct FontWeight
{
public System.UInt16 Weight;
}
If you set the XAML property on the control, it manages to convert the string to a FontWeights
static property.
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; }
}
However in the trigger you're stating that the string you're entering is actually a FontWeight
struct (and not a FontWeights
property), thus the system has an error trying to convert the string.
<Core:ChangePropertyAction.Value>
<FontWeight>ExtraBold</FontWeight>
</Core:ChangePropertyAction.Value>
Solution: You could use a GoToStateAction
and VisualStates to fix your issue.
<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>
And add the correct states to your container control.
<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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With