Wpf Conditional Converter
post_id: 111 / post_date: 2012-10-10
[tab:PS]
Sometimes the enabling/visibility of a control has to be based on whether a bounded property matches a value or not.
Using the System.Windows.Data.IValueConverter and making use of ConverterParameter, we can set this up generically to compare with the bounded value as shown in the code tab.
This is easier that setting up the code to programmatically do this / creating special properties like IsSomething. With judicious use of parameter name, this can be carried to any level.
[tab:Code]
// Usage ConditionalConverter.Register("IsSomething", "What1", false, true); ConditionalConverter.Register("NotSomethingElse", "What2", false, true); // In WPF // ctl IsEnabled="{Binding Path=ItemName, Converter={StaticResource ConditionalConverter}, ConverterParameter=IsSomething}" // In the Converter private static Dictionary<string, Tuple<object, object, object>> Conditions = new Dictionary<string,Tuple<object,object,object>>; public static void Register<T>(string parameter, object value, T valueIfEqual, T valueIfNotEqual) { Conditions[parameter] = new Tuple<object, object, object>(value, valueIfEqual, valueIfNotEqual); } public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { Tuple<object, object, object> condition; if (Conditions.TryGetValue((string)parameter, out condition) == false) { throw new InvalidOperationException("Not registered with Parameter:" + parameter); } return condition.Item1.Equals(value) ? condition.Item2 : condition.Item3; }