Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# WPF组合框,内有复选框_C#_Wpf_Checkbox_Combobox - Fatal编程技术网

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>

如果您不需要
组合框的选择功能
请参见我的答案