C# WPF组合框,内有复选框
组合框中有复选框,我想单击它们: 如果我单击复选框或复选框的“内容”,它将选中复选框,但如果我单击“内容”旁边的空白区域,它将在组合框中选中此复选框 我怎样才能防止呢?如果单击整个字段,而不仅仅是文本,我希望选中该框 这是我的密码:C# WPF组合框,内有复选框,c#,wpf,checkbox,combobox,C#,Wpf,Checkbox,Combobox,组合框中有复选框,我想单击它们: 如果我单击复选框或复选框的“内容”,它将选中复选框,但如果我单击“内容”旁边的空白区域,它将在组合框中选中此复选框 我怎样才能防止呢?如果单击整个字段,而不仅仅是文本,我希望选中该框 这是我的密码: <ComboBox Margin="2,2,2,0" ItemsSource="{Binding DataContext.AllTags, ElementName=self}" > <ComboBo
<ComboBox Margin="2,2,2,0" ItemsSource="{Binding DataContext.AllTags, ElementName=self}" >
<ComboBox.ItemTemplate>
<DataTemplate>
<CheckBox IsChecked="{Binding IsChecked}" Content="{Binding Name}">
<i:Interaction.Triggers>
<i:EventTrigger EventName="Checked">
<i:InvokeCommandAction Command="{Binding DataContext.CmdCmx_UpdateTags, ElementName=self}" CommandParameter="{Binding}" />
</i:EventTrigger>
<i:EventTrigger EventName="Unchecked">
<i:InvokeCommandAction Command="{Binding DataContext.CmdCmx_UpdateTags, ElementName=self}" CommandParameter="{Binding}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
</CheckBox>
</DataTemplate>
</ComboBox.ItemTemplate>
<i:Interaction.Triggers>
<i:EventTrigger EventName="DropDownOpened">
<i:InvokeCommandAction Command="{Binding DataContext.CmdCmx_ClearTags, ElementName=self}" />
</i:EventTrigger>
</i:Interaction.Triggers>
</ComboBox>
添加一个
ItemContainerStyle
,将ComboBoxItem
容器的HorizontalContentAlignment
属性设置为Stretch
:
<ComboBox Margin="2,2,2,0" ItemsSource="{Binding DataContext.AllTags, ElementName=self}" >
<ComboBox.ItemContainerStyle>
<Style TargetType="ComboBoxItem">
<Setter Property="HorizontalContentAlignment" Value="Stretch" />
</Style>
</ComboBox.ItemContainerStyle>
<ComboBox.ItemTemplate>
<DataTemplate>
<CheckBox IsChecked="{Binding IsChecked}" Content="Name">
<i:Interaction.Triggers>
<i:EventTrigger EventName="Checked">
<i:InvokeCommandAction Command="{Binding DataContext.CmdCmx_UpdateTags, ElementName=self}" CommandParameter="{Binding}" />
</i:EventTrigger>
<i:EventTrigger EventName="Unchecked">
<i:InvokeCommandAction Command="{Binding DataContext.CmdCmx_UpdateTags, ElementName=self}" CommandParameter="{Binding}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
</CheckBox>
</DataTemplate>
</ComboBox.ItemTemplate>
<i:Interaction.Triggers>
<i:EventTrigger EventName="DropDownOpened">
<i:InvokeCommandAction Command="{Binding DataContext.CmdCmx_ClearTags, ElementName=self}" />
</i:EventTrigger>
</i:Interaction.Triggers>
</ComboBox>
如果您不需要
组合框的选择功能
请参见我的答案