dolgom dolgom - 7 months ago 30
C# Question

WPF : Binding to which comoboboxItem is selected

I'd like to enable/disabled 'CheckBox' element according to which 'ComboBoxItem' is selected. I don't know how to implement this function by using WPF binding.

More specifically, here is my xaml code.

<ComboBox x:Name="typeComboBox" SelectedValuePath="Tag">
<ComboBoxItem Content="type1" Tag="1"></ComboBoxItem>
<ComboBoxItem Content="type2" Tag="2" IsSelected="True"></ComboBoxItem>
<CheckBox x:Name="mode" Content="Mode"
IsEnabled="{Binding ElementName=typeComboBox, Path=SelectedValue??}"/>

I want that only when 'type2' is selected, 'mode' is enabled. If 'type1' is selected, 'mode' should be disabled. Could I bind 'IsEnabled' property of 'CheckBox' to 'selectedValue' property of 'ComboBox'?

I had attempted to implemente this function as 'SelectionChanged' event, but 'NullReferenceException' occured. So I'm trying to make it by using WPF Binding.


This should work:

<ComboBox x:Name="typeComboBox" SelectedValuePath="Tag">
   <ComboBoxItem x:Name="box1" Content="type1" Tag="1"/>
   <ComboBoxItem x:Name="box2" Content="type2" Tag="2" IsSelected="True"/>
<CheckBox x:Name="mode" Content="Mode" IsEnabled="{Binding ElementName=box2, Path=IsSelected}"/>