How to enable wpf controls through data binding on a selected item from a combo box
-
I am looking for a way where a control can be enable when an item from a combo box is selected. Is there a simple way through data binding when a user selects an item from a combo box that it then enables another control to be used?
-
I am looking for a way where a control can be enable when an item from a combo box is selected. Is there a simple way through data binding when a user selects an item from a combo box that it then enables another control to be used?
One way is to bind IsEnabled on the control(s) to the ComboBox SelectedIndex or SelectedItem property using a value converter[^] on the binding to convert the Selectedxxx value to a bool.
\[System.Windows.Data.ValueConversion(typeof(int), typeof(bool))\] public class SelectedIndexToIsEnabledConverter : System.Windows.Data.IValueConverter { public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { return ((int)value >= 0) ? true : false; } public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { throw new System.NotImplementedException(); } } <UserControl.Resources > <local:SelectedIndexToIsEnabledConverter x:Key="SelectedIndexToIsEnabledConverter" /> </UserControl.Resources>
...
Example binding:
IsEnabled="{Binding Path=SelectedIndex,ElementName=comboBox1,Converter={StaticResource SelectedIndexToIsEnabledConverter},Mode=OneWay}"Mark Salsbery Microsoft MVP - Visual C++ :java:
modified on Friday, July 15, 2011 12:33 PM