tags:

views:

25

answers:

1

Hi there,

I have a question about CheckBox. I don't want the CheckBox to fire the checked event when the user clicks on the content of the CheckBox. I know that I can set IsHitTestVisible = False in the ContentPresenter.

At the moment I can't initialize my CheckBox with cb.IsChecked. Any ideas regarding this? Or has anyone a different idea?

Thanks in advance.

CU soltyr

My XAML Code:

 <Style x:Key="CheckBoxStyle" x:Name="myCheckBoxStyle" TargetType="{x:Type CheckBox}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type CheckBox}">
                    <Grid>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="16" />
                            <ColumnDefinition Width="Auto" />
                        </Grid.ColumnDefinitions>
                        <CheckBox Grid.Column="0" Click="cb_Click">
                        </CheckBox>
                        <ContentPresenter Grid.Column="1"
                          Margin="5,0,0,0"
                          VerticalAlignment="Center"
                          HorizontalAlignment="Left"
                          IsHitTestVisible="False" />
                        </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter> 
    </Style>

My C# Code

for (int i = 0; i < tcList.Count; i++)
        {
            lbi = new ListBoxItem();
            tc = (TableContent)tcList[i];
            cb = new CheckBox();
            cb.Style = (Style)FindResource("CheckBoxStyle");
            cb.Content = tc.test1;
            cb.IsChecked = tc.test2;
            lbi.Margin = new Thickness(0, 4, 0, 0);
            lbi.Content = cb;
            lbi.Selected += new RoutedEventHandler(lbi_Selected);
            lbi.KeyDown += new KeyEventHandler(lbi_KeyUp);
            lbi.KeyUp += new KeyEventHandler(lbi_KeyUp);
            lbTaster.Items.Add(lbi);
            ht.Add(i, tc.On_Off);
        }
A: 

You have created a CheckBox whose template contains a second CheckBox. You are setting IsChecked on the outer one, but this does not affect the inner one. The simplest fix is to bind the IsChecked property on the inner text box to the IsChecked property on the outer one:

<CheckBox Grid.Column="0" Click="cb_Click" IsChecked="{Binding IsChecked, 
    RelativeSource={RelativeSource TemplatedParent}}">
</CheckBox>
Quartermeister