Reputation: 1782
I have some checkboxes that are defined like this:
(This is the checkbox that you can click on to check/uncheck all the other checkboxes, let's call this *)
<CheckBox Content="Check all" Grid.Column="1" Grid.Row="1" Margin="200 7 0 0" Name="IsCheckedCheckAll"
Checked="IsCheckedCheckAll_Checked" Unchecked="IsCheckedCheckAll_Checked" />
And the C# code for the above, is:
if (IsCheckedCheckAll.IsChecked == true)
{
IsCheckedCheckAll.Content = "Uncheck all";
}
else
{
IsCheckedCheckAll.Content = "Check all";
}
(And below is the checkbox that depends on the one above, and I have several others like this, lets call this **)
<CheckBox Name="Exc2" Grid.Column="1" Grid.Row="3" Margin="200 7 0 0" IsChecked="{Binding IsChecked, ElementName=IsCheckedCheckAll, Mode=OneWay}" />
The thing to notice, is the binding. Say I have 10 other checkboxes that have registered to the binding. I can then check the * checkbox, so that all the others that have a binding gets checked. Now let's say that I want to uncheck the ** checkbox. This will still be noted as checked, so the value that is inserted in the database will be wrong.
My C# code for determining if the checkbox is checked looks like this:
bool ex2 = (bool) Exc2.IsChecked
This value will return false, if Exc1 checkbox indeed is checked, but the checkbox that do the binding is unchecked. This might seem like an easy fix, but I can't come up with it. I can fix it if I stop using bindings, but I want to use binding. Maybe I'm just using the binding in a wrong way?
I tried to explain the problem as good as I can, I hope it's understandable
Upvotes: 0
Views: 60
Reputation: 1782
The problem was that I had copy&past my code, so it looked like this:
bool ex1 = (bool) Exc1.IsChecked, ex2 = (bool) Exc1.IsChecked, ex3 = (bool) Exc1.IsChecked,
ex4 = (bool) Exc1.IsChecked, ex5 = (bool) Exc1.IsChecked;
Instead of:
bool ex1 = (bool) Exc1.IsChecked, ex2 = (bool) Exc2.IsChecked, ex3 = (bool) Exc3.IsChecked,
ex4 = (bool) Exc4.IsChecked, ex5 = (bool) Exc5.IsChecked;
Notice that the top code sets all the declarations of ex1, e2, ex3... to Exc1.Ischecked. This of course didn't work as expected, and I ended up using almost an hour finding out what was the problem.
Lesson learned. DON'T use c&p blindly :D
Upvotes: 0
Reputation: 4385
Things are working as expected for me. See sample below:
<Window x:Class="WpfApplication2.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<Style x:Key="SomeStyle">
<Style.Triggers>
<Trigger Property="CheckBox.IsChecked" Value="True">
<Trigger.Setters>
<Setter Property="CheckBox.Content" Value="Uncheck all"></Setter>
</Trigger.Setters>
</Trigger>
<Trigger Property="CheckBox.IsChecked" Value="False">
<Trigger.Setters>
<Setter Property="CheckBox.Content" Value="Check all"></Setter>
</Trigger.Setters>
</Trigger>
</Style.Triggers>
</Style>
</Window.Resources>
<Grid>
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<CheckBox Grid.Column="1" Grid.Row="0" Margin="200 7 0 0" Name="IsCheckedCheckAll" Style="{StaticResource SomeStyle}"/>
<CheckBox Name="Exc2" Grid.Column="1" Grid.Row="1" Margin="200 7 0 0" IsChecked="{Binding IsChecked, ElementName=IsCheckedCheckAll, Mode=OneWay}" />
<CheckBox Name="Exc3" Grid.Column="1" Grid.Row="2" Margin="200 7 0 0" IsChecked="{Binding IsChecked, ElementName=IsCheckedCheckAll, Mode=OneWay}" />
<Button Grid.Column="1" Grid.Row="3" Content="{Binding IsChecked, ElementName=IsCheckedCheckAll}" Click="ButtonBase_OnClick"></Button>
</Grid>
</Window>
namespace WpfApplication2
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void ButtonBase_OnClick(object sender, RoutedEventArgs e)
{
MessageBox.Show(((bool) Exc2.IsChecked).ToString());
}
}
}
Also you can use Styles and Triggers to auto change the text for * checkbox.
Upvotes: 1