Reputation: 735
Update: this issue is confirmed fixed on .NET 4.0 (I was using 3.5).
I'm trying to use a checkbox so that it can be set manually by the user, unless a combobox in the same Window has a specific value (in which case the checkbox is disabled and checked).
This works perfectly, until the user manually checks/unchecks the box. After that, the IsChecked=True setter stops working when the value in the combobox is changed. The IsEnabled=False setter continues to work as expected.
I ended up having to use some logic in the code-behind, when I had hoped it would be possible using pure Xaml.
Does anyone know if this is a bug or intended behaviour. And if so, why?
<CheckBox>
Some text here
<CheckBox.Style>
<Style TargetType="{x:Type CheckBox}">
<Setter Property="IsChecked" Value="False" />
<Setter Property="IsEnabled" Value="True" />
<Style.Triggers>
<DataTrigger Binding="{Binding Path=SelectedValue, ElementName=comboBox1, Mode=OneWay}" Value="Disable Checkbox">
<Setter Property="IsChecked" Value="True" />
<Setter Property="IsEnabled" Value="False" />
</DataTrigger>
</Style.Triggers>
</Style>
</CheckBox.Style>
</CheckBox>
Thanks.
Upvotes: 3
Views: 1648
Reputation: 6124
there is something else going on in your project.
I tried the following:
<Window x:Class="Test.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow"
Name="Window"
SizeToContent="WidthAndHeight">
<StackPanel>
<CheckBox>
some text here
<CheckBox.Style>
<Style TargetType="{x:Type CheckBox}">
<Setter Property="IsChecked" Value="False" />
<Setter Property="IsEnabled" Value="True" />
<Style.Triggers>
<DataTrigger Binding="{Binding Path=SelectedIndex, ElementName=combo, Mode=OneWay}" Value="2">
<Setter Property="IsChecked" Value="True" />
<Setter Property="IsEnabled" Value="False" />
</DataTrigger>
</Style.Triggers>
</Style>
</CheckBox.Style>
</CheckBox>
<ComboBox Name="combo">
<TextBlock>item 1</TextBlock>
<TextBlock>item 2</TextBlock>
<TextBlock>item 3</TextBlock>
<TextBlock>item 4</TextBlock>
<TextBlock>item 5</TextBlock>
</ComboBox>
</StackPanel>
</Window>
So very easy. The goal here, is to set exactly the same values as yours for IsEnable and IsChecked when the item 3 is selected in the comboBox
and it works like a charm. That is : the user check/uncheck the checkbox as he pleases, then when he choses "item 3" in the comboBox, the checkbox is checked and disabled. If he changes chosen item in the comboBox again, the Checkbox is unchecked and re-enabled and he can check/unckeck it again and again.
So there is something else going on for you. What you wrote should work.
Edit: With regards to your comment, I tested with the 3.5 version instead of the 4.0 I was first using and could indeed reproduce your issue, so this is clearly a bug that was fixed in the 4.0 version of the .net framework.
Upvotes: 2