Data Trigger using Converter not working
Asked Answered
V

3

4

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.

Venule answered 11/10, 2011 at 9:38 Comment(4)
What have you tried? have you set a breakpoint and saw that you reach the converter from the XAML?Annexation
Yes, the Converter firesVenule
What's the text of your textblock and what value is retuned by your converter?Unknot
text is double number and that is same that is recd. by the converter. The converter returns true or false depending on the value greater than 0 or not. While debugging the converter looks good.Venule
U
9
<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.

Unknot answered 11/10, 2011 at 10:16 Comment(7)
Is your Text value will always be a double value? If not the converter code might break at this line - if (System.Convert.ToDouble(value) >= 0) return true;Unknot
No, I have checks before that. if (value == null) return false; if (value.ToString().Length == 0) return false;Venule
But if text will be string say "Test" in that case it will bypass your two conditions and it won't be converted to double value!!Unknot
Highly unlikely, the text is bound to type double. There are no First chance Exceptions or anything weird in the output window.Venule
Can i see your textblock where you apply this style? Because for me its running fine as i posted.Unknot
Updated the Question with entire textblockVenule
Please see my answer, it should work now. You need to set the Foreground property in your Style setter.Unknot
K
1

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

Converter={StaticResource converters:ColorConverter}
Karlsruhe answered 11/10, 2011 at 9:49 Comment(1)
Still Does not work, even If define the converter as a static resource and set the converter as its resource key.Venule
U
0

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.

Uther answered 4/10, 2018 at 4:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.