Search code examples
silverlightdata-bindingxamltriggersdatatrigger

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!


Solution

  • 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}}">