开发者

Changing ForeColor of a Textblock in Silverlight4 according to a value

I'm tryng to do a should-be-easy stuff and unfortunately I'can't figura out ho开发者_开发百科w: I want a TextBlock diplaying positive numers in White and negative numer in red ( pretty original, ehm ) I would like this stuff be implemnented just the view side. I see VisualStateManager, but apparently I need to drive it in the code behind to manually change the state. There should be something easyer: in WPF I would use a trigger with a ValueConverter, is there something similar in Silverlight ?


It is the same in Silverlight. Create a class that implements IValueConverter, add it as a resource in your control. Bind your brush to the property of interest, then set the Binding expressions Converter property to your resource. Done.

    public class BrushColorConverter : IValueConverter
  {

    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        return ((int)value >= 0) ? new SolidColorBrush(Colors.White) : new SolidColorBrush(Colors.Red);
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }
  }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜