WPF trigger that will work if the value is equal to or greater

I wrote an application in WPF with a button and a slider. I would like to create a trigger for a button that sets the "IsEnable" property of the "false" button when the value of the slider is greater than another value. Right now I have:

<Style x:Key="zoomOutButton" TargetType="Button" BasedOn="{StaticResource ResourceKey=buttonStyle}"> <Style.Triggers> <DataTrigger Binding="{Binding CurrentAltitude}" Value="24000"> <Setter Property="IsEnabled" Value="False" /> </DataTrigger> </Style.Triggers> </Style> 

But I would like to set isEnable if CurrentAltitude is 24000, but when it is equal to or greater than 24000. Any ideas?

+8
c # triggers visual-studio-2010 wpf
source share
2 answers

You can achieve this with a converter:

 public class IsEqualOrGreaterThanConverter : IValueConverter { public static readonly IValueConverter Instance = new IsEqualOrGreaterThanConverter(); public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { int intValue = (int) value; int compareToValue = (int) parameter; return intValue >= compareToValue; } public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) { throw new NotImplementedException(); } } 

Then your trigger will look like this:

 <Style x:Key="zoomOutButton" TargetType="Button" BasedOn="{StaticResource ResourceKey=buttonStyle}"> <Style.Triggers> <DataTrigger Binding="{Binding CurrentAltitude, Converter={x:Static my:IsEqualOrGreaterThanConverter.Instance}, ConverterParameter=24000}" Value="True"> <Setter Property="IsEnabled" Value="False" /> </DataTrigger> </Style.Triggers> </Style> 
+13
source share

A more general converter used with any comparable type could be:

 public class IsGreaterOrEqualThanConverter : IValueConverter { public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { IComparable v = value as IComparable; IComparable p = parameter as IComparable; if (v == null || p == null) throw new FormatException("to use this converter, value and parameter shall inherit from IComparable"); return (v.CompareTo(p) >= 0); } public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { throw new NotImplementedException(); } } 

But in this case, the ConverterParameter parameter should be interpreted with the same type as the value passed to your converter. For example, to compare the int property "MyIntProperty" with the number contant int 1, in your XAML you can use this syntax:

 <UserControl x:Class="MyNamespace.MyControl" xmlns:sys="clr-namespace:System;assembly=mscorlib" xmlns:genconverters="clr-namespace:MyConverterNamespace;assembly=MyConvertersAssembly"> <Grid> <Grid.Resources> <genconverters:IsGreaterOrEqualThanConverter x:Key="IsEqualOrGreaterThanConverter"/> <sys:Int32 x:Key="Int1">1</sys:Int32> </Grid.Resources> <ComboBox IsEnabled="{Binding MyIntProperty, Converter={StaticResource IsEqualOrGreaterThanConverter}, ConverterParameter={StaticResource Int1}}" ItemsSource="{Binding Items}" SelectedItem="{Binding SelectedItem}"/> </Grid> 

+1
source share

All Articles