1
我对CheckBox有疑问。我不希望CheckBox在用户点击CheckBox的内容时触发checked事件。我知道我可以在ContentPresenter中设置IsHitTestVisible = False。CheckBox no checked鼠标点击内容时的事件
目前我无法用cb.IsChecked初始化我的CheckBox。有关这个的任何想法?或者有任何人有不同的想法?
在此先感谢。
CU soltyr
我的XAML代码:
<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>
我的C#代码
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);
}