用于更改Textblock前景的转换器 [英] Converter for changing Textblock Foreground

查看:77
本文介绍了用于更改Textblock前景的转换器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hello Everybody !!

Hello Everybody !!

我需要动态更改文本块的前景。

I need to change dynamically the foreground of a textblock.

我开发了一个转换器:

 public class BooleanToColorConverter : IValueConverter
  {

    public object Convert(object value, Type targetType, object parameter, string language)
    {
      if (value != null)
      {
        bool val = (bool)value;
        if (parameter != null && string.Compare(parameter as string, "calendar", StringComparison.CurrentCultureIgnoreCase) == 0)
        {
          return val == true ? new SolidColorBrush(Color.FromArgb(0, 255, 0, 0)) : new SolidColorBrush(Color.FromArgb(0, 140,140,140));
        }
      
      }
      return false;
    }

    public object ConvertBack(object value, Type targetType, object parameter, string language)
    {
      throw new NotImplementedException();
    }
  }

这是我的XAML代码:

And this is my XAML Code :

                <TextBlock TextWrapping="Wrap" Grid.Column="1"
                           Foreground="{Binding IsWriteInRed, Converter={StaticResource BooleanToColorConverter}, ConverterParameter=calendar}" 
                           SelectionHighlightColor="{StaticResource WhiteSolidColorBrush}" Margin="5,0" VerticalAlignment="Center">
                  <Run Text="{Binding Company.Name}"/>
                  <Run Text=":"/>
                  <Run Text="{Binding Subject}"/>
                </TextBlock>

它不起作用,我不知道为什么......有任何想法吗?

It doesn't work, I don't know why...any ideas please??

谢谢

推荐答案

如果对IsWriteInRed属性的绑定失败,即无法在TextBlock的DataContext上找到该属性,则永远不会调用转换器。如果绑定失败,您可以指定要使用的FallbackValue:

If the binding to the IsWriteInRed property fails, i.e. the property cannot be found on the DataContext of the TextBlock, the converter will never be invoked. You can specify a FallbackValue to be used if the binding fails:

        <TextBlock TextWrapping="Wrap" Grid.Column="1"
                           Foreground="{Binding IsWriteInRed, Converter={StaticResource BooleanToColorConverter}, ConverterParameter=calendar
            ,FallbackValue=Red}" 
                           SelectionHighlightColor="{StaticResource WhiteSolidColorBrush}" Margin="5,0" VerticalAlignment="Center">
                  <Run Text="{Binding Company.Name}"/>
                  <Run Text=":"/>
                  <Run Text="{Binding Subject}"/>
        </TextBlock>

如果找到IsWriteInRed属性,它应该有效。尽管从Convert方法返回false没有多大意义:

If the IsWriteInRed property is found it should work. It doesn't make much sense to ever return false from the Convert method though:

        public object Convert(object value, Type targetType, object parameter, string language)
        {
            bool val = (bool)value;
            if (parameter != null && string.Compare(parameter as string, "calendar", StringComparison.CurrentCultureIgnoreCase) == 0
                && val)
            {
                return new SolidColorBrush(Color.FromArgb(0, 255, 0, 0));
            }

            return new SolidColorBrush(Color.FromArgb(0, 140, 140, 140));
        }

 



请记住通过将有用的帖子标记为答案来关闭您的主题,如果您有新问题,请开始新的主题。


Please remember to close your threads by marking helpful posts as answer and then please start a new thread if you have a new question.


这篇关于用于更改Textblock前景的转换器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆