【问题标题】:Check checkboxes in combobox选中组合框中的复选框
【发布时间】:2014-10-21 13:31:59
【问题描述】:

我有一个组合框,其中有复选框作为它的 combobox.itemtemplate。

<ComboBox Name="comboBoxTest" 
                          SelectedValuePath="Test" 
                          SelectedItem="{Binding SelectedTest, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
                          SelectedValue="{Binding SelectedTest, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
                          ItemsSource="{Binding Test, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" 
                          TextBoxBase.TextChanged ="comboBoxTest_TextChanged" Grid.ColumnSpan="2"
                          TextSearch.TextPath="Model" >
                    <ComboBox.ItemTemplate>
                        <DataTemplate>
                            <CheckBox Name="checkBoxTest"
                                      Content="{Binding Test}"
                                      Click="checkBoxTest_Click"/>
                        </DataTemplate>
                    </ComboBox.ItemTemplate>
                </ComboBox>

生成结果列表时,已将“--Select All--”项添加到结果列表中。

当用户选中“全部”项时,其他复选框也应选中。 我正在使用下面的代码,但它不起作用。

if (checkBoxTest.Content.ToString().Equals("--Select All--"))
{
     foreach (object item in comboBoxTest.Items)
     {
         ComboBoxItem comboBoxItem = comboBoxTest.ItemContainerGenerator.ContainerFromItem(item) as ComboBoxItem;
          FrameworkElement element = comboBoxItem.ContentTemplate.LoadContent() as FrameworkElement;
          CheckBox checkBox = element.FindName("checkBoxTest") as CheckBox;
          checkBox.IsChecked = true;
      }
 }

【问题讨论】:

    标签: c# wpf checkbox combobox


    【解决方案1】:

    您的代码中存在一些问题,让我先告诉您这些问题。

    1. 识别“全选”复选框的 if 条件不正确。您需要使用 Contains() 而不是 equals()
    2. 您提取的复选框不是组合框项中的正确复选框。如果您尝试查看 checkBox.Content 属性,您将看到 null 作为结果。

    当“全选”复选框被选中时,请参阅下面的代码来选择组合框中的所有复选框。

    您的 Checkbox Click 事件应如下所示。

    private void checkBoxTest_Click(object sender, RoutedEventArgs e)
    {
                CheckBox checkBoxTest = e.Source as CheckBox;
                if (checkBoxTest == null)
                    return;
    
                if (checkBoxTest.Content.ToString().Contains("Select All") && checkBoxTest.IsChecked == true)
                {
                    foreach (object item in comboBoxTest.Items)
                    {
                        ComboBoxItem comboBoxItem = comboBoxTest.ItemContainerGenerator.ContainerFromItem(item) as ComboBoxItem;                   
                        if (comboBoxItem == null)
                        {
                            return;
                        }
                        CheckBox checkBox = FindVisualChildByName<CheckBox>(comboBoxItem, "checkBoxTest");
                        checkBox.IsChecked = true;
                    }
                }
    }
    

    我添加了一种新方法,可以从子元素的名称及其类型中获取任何元素中的可视子元素。

    private static T FindVisualChildByName<T>(DependencyObject parent, string name) where T : DependencyObject
            {
                for (int i = 0; i < VisualTreeHelper.GetChildrenCount(parent); i++)
                {
                    var child = VisualTreeHelper.GetChild(parent, i);
                    string controlName = child.GetValue(NameProperty) as string;
                    if (controlName == name)
                    {
                        return child as T;
                    }
                    T result = FindVisualChildByName<T>(child, name);
                    if (result != null)
                        return result;
                }
                return null;
            }
    

    【讨论】:

    • 谢谢!你的代码非常适合我。我之前被困在visualtreehelper上。感谢您引导我正确的方式
    • 它也适用于具有更多项目以显示垂直滚动条的组合框。只需验证组合框的代码是否有足够的项目在弹出窗口中显示垂直滚动条
    • 好吧,我刚刚试过了。当有垂直滚动条时它确实有效。我以为 ComboBox 的ItemsPanel 使用了VirtualizingStackPanel,这样就不行了。
    • @KingKing 也感谢您的建议。如果我下次使用 VirtualizingStackPanel,我会处理它。
    • @user1850936 如果使用了VirtualizingStackPanel,您可以通过将附加属性VirtualizingPanel.IsVirtualizing 设置为false 来关闭它。通常一个组合框不包含很多项目,这就是为什么默认不使用VirtualizingStackPanel
    【解决方案2】:

    【讨论】:

    • 感谢您的努力!我以前研究过这些,但我试图在我的帖子中产生类似的东西。 =)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-27
    • 2011-07-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多