使用Converter的数据触发不起作用 [英] Data Trigger using Converter not working

查看:117
本文介绍了使用Converter的数据触发不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试根据其值更新文本块的颜色。似乎很简单,但是不起作用。

I am trying to update the color of textblock depending on it value. Seems simple however not working.

这是文本块xaml。

 <TextBlock 
        Grid.Column="1" 
        Grid.Row="1" 
        Text="{Binding Path=GL, StringFormat={}{0:N0}}" 
        HorizontalAlignment="Left" 
        FontFamily="Verdana" 
        Foreground="Tomato" 
        FontWeight="Bold"             
        VerticalAlignment="Center"
        Margin="5,2,5,0"
        FontSize="18"
        >
        <TextBlock.Resources>
            <converters:ColorConverter x:Key="CoConverter"></converters:ColorConverter>
        </TextBlock.Resources>
      <TextBlock.Style>
          <Style>
              <Style.Triggers>
                  <DataTrigger Binding="{Binding RelativeSource={RelativeSource Self}, Path=Text, Converter={StaticResource ResourceKey=CoConverter}}" Value="true">
                  <Setter Property="TextBlock.Foreground" Value="LimeGreen" />
                      </DataTrigger>
              </Style.Triggers>
          </Style>
      </TextBlock.Style>

此处是转换器

public class ColorConverter : MarkupExtension, IValueConverter
{

    #region IValueConverter Members

    public object Convert(object value, 
        Type targetType, 
        object parameter, 
        System.Globalization.CultureInfo culture)
    {
        if (value == null)
            return false;
        if (value.ToString().Length == 0)
            return false;
        if (System.Convert.ToDouble(value) >= 0)
            return true;

        return false;
    }

    public override object ProvideValue(IServiceProvider serviceProvider)
    {
        return this;
    }
}

转换器看起来不错,但触发器未应用

The converter looks good, however the trigger is not applying for some reason.

推荐答案

<TextBlock 
        Grid.Column="1" 
        Grid.Row="1" 
        Text="{Binding Path=GL, StringFormat={}{0:N0}}" 
        HorizontalAlignment="Left" 
        FontFamily="Verdana"
        FontWeight="Bold"             
        VerticalAlignment="Center"
        Margin="5,2,5,0"
        FontSize="18"
        >
        <TextBlock.Resources>
            <converters:ColorConverter x:Key="CoConverter"></converters:ColorConverter>
        </TextBlock.Resources>
      <TextBlock.Style>
          <Style>
              <Setter Property="TextBlock.Foreground" Value="Tomato" />
              <Style.Triggers>
                  <DataTrigger Binding="{Binding RelativeSource={RelativeSource Self}, Path=Text, Converter={StaticResource ResourceKey=CoConverter}}" Value="true">
                  <Setter Property="TextBlock.Foreground" Value="LimeGreen" />
                      </DataTrigger>
              </Style.Triggers>
          </Style>
      </TextBlock.Style>

您需要以自己的样式设置Foreground属性,以便在运行时动态更改它。

You need to set the Foreground property in your style to change it dynamically at runtime.

这篇关于使用Converter的数据触发不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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