Data Trigger using Converter not working

wpf converter
xaml converter
the typeconverter for ivalueconverter does not support converting from a string
ivalueconverter wpf mvvm

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.

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

DataTrigger and ValueConverter to color negative numbers red in , In one of the application I am working, lot of converters were used for favor of triggers at present but some problems I see in using converters are – Actually, “​converters are for converting one data type into another” not for  The issue here is Property Value Precedence.. You are currently setting the Background to blue directly on the DockPanel. This explicit property will override any value set by the trigger.

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

Converter={StaticResource converters:ColorConverter}

https://social.msdn.microsoft.com/Forums/vstudio/e, You can even use value converters with the data binding. This means that you can work around the limitation of detecting a single value. just get rid of the has data property all together. Don't rly need it from what we see of your code. First set a Style Default Setter for Visibility as Visible. Next you only need one Trigger. Use a ElementName=searchBox, Path=Text binding and check if Value={x:Static sys:String.Empty} and switch the Visibility to Hidden. DP or INPC for a property that isnt even needed is just overkill.

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.

akjoshi's Blog, Trigger Binding to Child Control Not Working #2069 Data Information: 10 : Cannot retrieve value using the binding and no valid If you want to debug it put a {Binding RelativeSource=xxx, Converter=YourDummyConverter}  Gets a collection of TriggerAction objects to apply when the trigger object becomes active. This property does not apply to the EventTrigger class. (Inherited from TriggerBase) ExitActions: Gets a collection of TriggerAction objects to apply when the trigger object becomes inactive. This property does not apply to the EventTrigger class.

WPF Triggers - Data Triggers, Vb.net WPF datatrigger not working if trigger value is integer Even if I convert the Integer to String via .ToString() or CType() it Instead, use:. 4 Answers 4. You could create an IValueConverter, which converts an integer to a boolean based on the CutOff. Then use DataTrigger.Value of True (or False, depending on what you are returning). WPF DataTriggers are strictly equality comparers if I remember correctly.

Trigger Binding to Child Control Not Working · Issue #2069 · dotnet , Checking DataTrigger binding against null value does not work in Just FYI - You can use *Converter* to achieve exact what you need in  Assuming your DataGridColumns are text-based then you can use an IValueConverter as below: Note that if some of data-grid columns are not text-based, this solution still works for those columns which are. Xaml: <Window.Resources> <local:FontWeightConverter x:Key="fontWeightConverter"/> </Window.Resources>

Vb.net WPF datatrigger not working if trigger value is integer , The DataTrigger does not always set the correct value. I see this issue You should use a converter in your Binding instead of a DataTrigger. A Trigger is typically used in a Style or ControlTemplate. It triggers on properties of whatever is being templated, and sets other properties of the control (or of specific template elements). For example, you would use a Trigger on IsMouseOver to respond to the mouse being over the control, and the setters might update a brush to show a "hot

Comments
  • What have you tried? have you set a breakpoint and saw that you reach the converter from the XAML?
  • Yes, the Converter fires
  • What's the text of your textblock and what value is retuned by your converter?
  • 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.
  • 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;
  • No, I have checks before that. if (value == null) return false; if (value.ToString().Length == 0) return false;
  • 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!!
  • Highly unlikely, the text is bound to type double. There are no First chance Exceptions or anything weird in the output window.
  • Please see my answer, it should work now. You need to set the Foreground property in your Style setter.
  • Still Does not work, even If define the converter as a static resource and set the converter as its resource key.