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>
La ponse
source share