0
votes

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>
</ComboBox>
<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.

2

2 Answers

2
votes

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"/>
</ComboBox>
<CheckBox x:Name="mode" Content="Mode" IsEnabled="{Binding ElementName=box2, Path=IsSelected}"/>
0
votes

Try this:

<ComboBox x:Name="combo">
    <ComboBoxItem x:Name="type1" Content="type1" IsSelected="True"></ComboBoxItem>
    <ComboBoxItem x:Name="type2" Content="type2"></ComboBoxItem>
</ComboBox>

<CheckBox>
    <CheckBox.Style>
        <Style>
            <Style.Triggers>
                <DataTrigger Binding="{Binding IsSelected, ElementName=type2}" Value="True">
                    <Setter Property="CheckBox.IsEnabled" Value="True"></Setter>
                </DataTrigger>
                <DataTrigger Binding="{Binding IsSelected, ElementName=type1}" Value="True">
                    <Setter Property="CheckBox.IsEnabled" Value="False"></Setter>
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </CheckBox.Style>
</CheckBox>