Data Trigger using Converter not working

2019-08-14 05:01发布

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

Here's the textblock 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>

Here the converter

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.

3条回答
迷人小祖宗
2楼-- · 2019-08-14 05:02

If you are trying to change property value dynamically, then corresponding property must be kept only in Setter tag.

<TextBlock>
  <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>

But not in both TextBlock and Setter tag. To be precise for your example remove Foreground property inside the TextBlock tag as given in my code.

查看更多
劫难
3楼-- · 2019-08-14 05:20
<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>

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

查看更多
仙女界的扛把子
4楼-- · 2019-08-14 05:29

Looks like you're missing StaticResource inside your curly braces when specifying the converter:

Converter={StaticResource converters:ColorConverter}
查看更多
登录 后发表回答