How to get DataTemplate.DataTrigger to check for greater than or less than?

ε祈祈猫儿з 提交于 2019-11-27 14:33:57
Mikko Rantanen

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.

So something similar to:

public class CutoffConverter : IValueConverter {
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture) {
        return ((int)value) > Cutoff;
    }

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

    public int Cutoff { get; set; }
}

Then use the following XAML.

<Window.Resources>
    <myNamespace:CutoffConverter x:Key="AgeConverter" Cutoff="30" />
</Window.Resources>

<DataTemplate.Triggers>
    <DataTrigger Binding="{Binding Path=Age,
                                   Converter={StaticResource AgeConverter}}">
        <DataTrigger.Value>true</DataTrigger.Value>
        <Setter TargetName="Age" Property="Foreground" Value="Red"/> 
    </DataTrigger>
</DataTemplate.Triggers>
Drew McGhie

I'd recommend using an IValueConverter to bind to the Foreground element of the Age TextBlock and isolating the coloring logic there.

<TextBlock x:Name="Age" Grid.Column="1" Grid.Row="2" Text="{Binding Age}" Foreground="{Binding Path=Age, Converter={StaticResource AgeToColorConverter}}" Margin="5"/>

Then in the Code:

[ValueConversion(typeof(int), typeof(Brush))]
public class AgeToColorConverter : IValueConverter
{
   public object Convert(object value, Type target)
   {
      int age;
      Int32.TryParse(value.ToString(), age);
      return (age >= 30 ? Brushes.Red : Brushes.Black);
   }
}

I believe there is a simpler way of acheiving the goal by using the powers of MVVM and INotifyPropertyChanged.


With the Age property create another property which will be a boolean called IsAgeValid. The IsAgeValid will simply be an on demand check which does not technically need an the OnNotify call. How?

To get changes pushed to the Xaml, place the OnNotifyPropertyChanged for IsAgeValid within the Age setter instead.

Any binding to IsAgeValid will have a notify message sent on any Age change which is really what is being looked at.


Once setup, of course bind the style trigger for false and true accordingly to the IsAgeValid result.

public bool IsAgeValid{ get { return Age > 30; } }

public int Age
{ 
  get { return _Age; }

  set
  {
   _Age=value;
   OnPropertyChanged("Age");   
   OnPropertyChanged("IsAgeValid"); // When age changes, so does the
                                    // question *is age valid* changes. So 
                                    // update the controls dependent on it.
   } 
 }

If possible, you could add a property to your model, that's the easiest way. Eg.

public int AgeBoundry
{
    get
    {
        if (Age < 30)
            return 0;
        else if (Age == 30)
            return 1;
        else
            return 2;
    }
}

then you can check on the value of the integer.

<DataTemplate.Triggers>
    <DataTrigger Binding="{Binding Path=Age}">
        <DataTrigger.Value>0</DataTrigger.Value>
        <Setter TargetName="Age" Property="Foreground" Value="Green"/> 
    </DataTrigger>
    <DataTrigger Binding="{Binding Path=Age}">
        <DataTrigger.Value>1</DataTrigger.Value>
        <Setter TargetName="Age" Property="Foreground" Value="Orange"/> 
    </DataTrigger>
    <DataTrigger Binding="{Binding Path=Age}">
        <DataTrigger.Value>2</DataTrigger.Value>
        <Setter TargetName="Age" Property="Foreground" Value="Red"/> 
    </DataTrigger>
</DataTemplate.Triggers>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!