Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bind FontWeight to a Boolean in Silverlight

Silverlight does not feature DataTriggers, so in this case... what might be the best way to conditionally set the fontweight of an item to a boolean?

For example... the following is not possible in Silverlight.

<TextBlock Text="{Binding Text}">
    <TextBlock.Triggers>
        <DataTrigger Binding="{Binding IsDefault}" Value="True">
            <Setter Property="FontWeight" Value="Bold"/>
        </DataTrigger>
        <DataTrigger Binding="{Binding IsDefault}" Value="False">
            <Setter Property="FontWeight" Value="Normal"/>
        </DataTrigger>
    </TextBlock.Triggers>
</TextBlock>

Thanks!

like image 947
Erik Kerber Avatar asked Dec 02 '22 06:12

Erik Kerber


1 Answers

You could implement a IValueConverter that converts a bool to a FontWeight, and use it as the binding's converter :

<UserControl.Resources>
    <local:BoolToFontWeightConverter x:Key="boolToFontWeight"/>
</UserControl.Resources>

...

<TextBlock Text="{Binding Text}" FontWeight="{Binding IsDefault, Converter={StaticResource boolToFontWeight}}">
like image 123
Thomas Levesque Avatar answered Dec 05 '22 01:12

Thomas Levesque