【问题标题】:WPF Combobox with checkboxes inside [duplicate]带有复选框的WPF组合框[重复]
【发布时间】:2018-12-11 15:38:53
【问题描述】:

我在 Combobox 中有复选框,我想点击它们:

如果我单击复选框或复选框的“内容”,它将选中该复选框,但如果我单击“内容”旁边的空白区域,它将仅选中组合框中的此复选框。

如何防止这种情况发生?如果我单击整个字段,而不仅仅是文本,我想选中该框。

这是我的代码:

 <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>

【问题讨论】:

  • 如果您不需要ComboBox 的选择功能,请参阅我的答案here 作为替代

标签: c# wpf checkbox combobox


【解决方案1】:

添加一个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>

【讨论】:

    猜你喜欢
    • 2013-06-15
    • 1970-01-01
    • 2011-09-24
    • 1970-01-01
    • 2014-10-14
    • 2018-10-19
    • 2014-12-25
    • 2020-04-01
    • 1970-01-01
    相关资源
    最近更新 更多