【问题标题】:ComboBox of CheckBoxes not checking item when row clicked单击行时复选框的组合框不检查项目
【发布时间】:2017-05-18 12:57:34
【问题描述】:

我有一个复选框组合框,允许用户选择多个温度范围。当用户单击复选框或复选框旁边的文本时,复选框会被正确选中,并且我的组合框文本会更新以添加/删除选定的温度。但是,如果我检查组合框下拉到文本右侧的区域,则不会切换复选框,而是在组合框中获得命名空间文本。

这是我的 XAML

<ComboBox x:Name="cbDegrees" ItemsSource="{Binding m_DegreeValues}" Text="Degrees" IsEditable="True" IsReadOnly="True" Grid.Row="0" Grid.Column="0" Margin="5" Background="Gray" >
                <ComboBox.ItemTemplate>
                    <DataTemplate>
                        <CheckBox Name="chkDegrees" Content="{Binding Name}" Checked="Degrees_CheckBox_Click" Unchecked="Degrees_CheckBox_Click"  IsChecked="{Binding Path=IsSelected, Mode=TwoWay}">
                        </CheckBox>
                    </DataTemplate>
                </ComboBox.ItemTemplate>
</ComboBox>

我希望在单击温度选项行中的任意位置时切换复选框。

【问题讨论】:

  • ComboBox 不是任何类型的通用下拉按钮。它是一个选择器,旨在为您提供从集合中选择项目的方法。如果您愿意,您可以简单地将 ToggleButton 和 Popup 放在一起并将它们绑定在一起。在您的弹出窗口中放置一个复选框列表。
  • 谢谢@Mishka,我会将您的方法视为一种可能的解决方案,但我已经在我的复选框组合框中拥有了我想要的所有其他功能。我只专注于修复这个缺陷,其他人在描述我遇到的问题并提供解决方案方面做得非常出色。
  • 我明白了,没关系,我只是对一些事情感到厌烦,这让我很烦。

标签: wpf xaml checkbox combobox


【解决方案1】:

生成的默认ComboBoxItem 未设置为拉伸并填充所有可用空间。如果您将 ComboBox 包装在类似 DockPanel 的东西中并设置了 Background 属性,您可以看到这一点。

<ComboBox.ItemTemplate>
    <DataTemplate>
        <DockPanel Background="CornflowerBlue">
            <CheckBox Name="chkDegrees" Content="{Binding Name}" .. />
        </DockPanel>
    </DataTemplate>
</ComboBox.ItemTemplate>

要修复它,请设置您的 ComboBoxItem 样式,以便将 HorizontalContentAlignment 设置为 Stretch

<ComboBox.Resources>
    <Style TargetType="{x:Type ComboBoxItem}">
        <Setter Property="HorizontalContentAlignment" Value="Stretch" />
    </Style>
</ComboBox.Resources>

【讨论】:

  • 谢谢瑞秋。我很欣赏示例代码,因此我可以看到停靠面板内复选框项目的实际宽度。您的解决方案非常适合我。
【解决方案2】:

您可以定义一个ItemContainerStyle,使CheckBox 水平伸展:

<ComboBox x:Name="cbDegrees" ItemsSource="{Binding m_DegreeValues}" Text="Degrees" IsEditable="True" IsReadOnly="True" Grid.Row="0" Grid.Column="0" Margin="5" Background="Gray" >
    <ComboBox.ItemContainerStyle>
        <Style TargetType="ComboBoxItem">
            <Setter Property="HorizontalContentAlignment" Value="Stretch" />
        </Style>
    </ComboBox.ItemContainerStyle>
    <ComboBox.ItemTemplate>
        <DataTemplate>
            <CheckBox Name="chkDegrees" Content="{Binding Name}" Checked="Degrees_CheckBox_Click" Unchecked="Degrees_CheckBox_Click"  IsChecked="{Binding Path=IsSelected, Mode=TwoWay}">
            </CheckBox>
        </DataTemplate>
    </ComboBox.ItemTemplate>
</ComboBox>

【讨论】:

    猜你喜欢
    • 2011-12-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-20
    • 2018-10-09
    • 2012-04-18
    相关资源
    最近更新 更多