views:

721

answers:

3

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!

+2  A: 

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}}">
Thomas Levesque
Ugh... FINE, I'll do all that work :)
eskerber
A: 

Create a custom IValueConverter, bind the FontWeight to IsDefault, and convert true to Bold and false to Normal

Rich
A: 

Hi, I have a string that either has "FontWeight_Bold" as its Value.

my xaml is...

my converter is... using System; using System.Globalization; using System.Windows;

namespace Thomson.MIS.Sales.Assistant.UI { /// /// A font weight converter /// public class StringToFontWeightConverter { /// /// /// /// /// /// /// /// protected FontWeight Convert(object value, Type targetType, object parameter, CultureInfo culture) { return (string)value == "Font_Bold" ? FontWeights.Bold : FontWeights.Normal; }

  /// <summary>
  /// 
  /// </summary>
  /// <param name="value"></param>
  /// <param name="targetType"></param>
  /// <param name="parameter"></param>
  /// <param name="culture"></param>
  /// <returns></returns>
  public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
  {
     return (bool)value ? "Font_Bold" : "Font_Normal";
  } 

} }

but i cant get the xaml to recognise the value converter... any suggestions?

Simon Hurley