Reputation: 9080
I have 2 RadioButton controls along with 2 ComboBox controls.
The user has the option of selecting Option 1 or Option 2. If Option 1 is selected I want the combobox to be disabled and if there was a selected value in the combobox I want it to be cleared. And vice versa if Option 2 is selected.
<RadioButton Grid.Row="1" Grid.Column="0" Checked="rbOption1Checked" >
<TextBlock HorizontalAlignment="Right" Text="Option 1 (Optional):" VerticalAlignment="Top"/>
</RadioButton>
<ComboBox Grid.Row="1" Grid.Column="2" VerticalAlignment="Top" Name="cboOption1" SelectedItem="{Binding SelectedOption1, UpdateSourceTrigger=PropertyChanged}" IsEnabled="{Binding IsRegionSelected}"/>
<RadioButton Grid.Row="2" Grid.Column="0" HorizontalAlignment="Left" Checked="rbOption2Checked">
<TextBlock HorizontalAlignment="Right" Text="Option 2 (Optional):" VerticalAlignment="Top"/>
</RadioButton>
<ComboBox Grid.Row="2" Grid.Column="2" VerticalAlignment="Top" Name="cboOption2" SelectedItem="{Binding SelectedOption2, UpdateSourceTrigger=PropertyChanged}" IsEnabled="{Binding IsRegionSelected}" />
I'd like to know how this could be accomplished with a DataTrigger. I don't know where to start on this. I've tried something like this but nothing happned.
<RadioButton Grid.Row="2" Grid.Column="0" HorizontalAlignment="Left" Checked="rbOption2Checked">
<TextBlock HorizontalAlignment="Right" Text="Option 2 (Optional):" VerticalAlignment="Top"/>
</RadioButton>
<ComboBox Grid.Row="2" Grid.Column="2" VerticalAlignment="Top" Name="cboOption2" SelectedItem="{Binding SelectedOption2, UpdateSourceTrigger=PropertyChanged}" IsEnabled="{Binding IsRegionSelected}">
<ComboBox.Style>
<Style TargetType="ComboBox">
<Style.Triggers>
<DataTrigger Binding="{Binding Path=IsChecked,ElementName=cboOption1}" Value="True">
<Setter Property="ComboBox.IsEnabled" Value="False"/>
</DataTrigger>
</Style.Triggers>
</Style>
</ComboBox.Style>
</ComboBox>
Upvotes: 2
Views: 4578
Reputation: 2682
This can definitely be done through a trigger. I think the first problem that you're having, and it's a common one, is that you're setting a property directly on your control and then trying to override it in the Style
; the direct setting is going to override the Style. This may be why the IsEnabled
property is never changing.
Here's the xaml for a RadioButton
and a ComboBox
, where the combo is only enabled and its selected item is only set when the corresponding button is checked
<RadioButton x:Name="radioButton1" Grid.Row="1" Grid.Column="0" >
<TextBlock HorizontalAlignment="Right" Text="Option 1 (Optional):" VerticalAlignment="Top"/>
</RadioButton>
<ComboBox Grid.Row="1" Grid.Column="2" VerticalAlignment="Top" Name="cboOption1" ItemsSource="{Binding ComboItems}">
<ComboBox.Style>
<Style TargetType="ComboBox">
<Setter Property="SelectedItem" Value="{Binding SelectedOption1, UpdateSourceTrigger=PropertyChanged}" />
<Style.Triggers>
<DataTrigger Binding="{Binding Path=IsChecked,ElementName=radioButton1}" Value="False">
<Setter Property="IsEnabled" Value="False"/>
<Setter Property="SelectedItem" Value="{x:Null}" />
</DataTrigger>
</Style.Triggers>
</Style>
</ComboBox.Style>
</ComboBox>
What the Style
is doing here is setting a default binding to the DataContext. When radioButton1
isn't checked, it's disabling the control and removing the binding; this does not change anything in the underlying ViewModel. When the button is checked, the binding is going to be set again and your control will reflect whatever value was originally selected.
Upvotes: 6