-2

I have a combobox which has checkbox as its 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>

An “--Select All--” item has been add into the result list when the result list is produced. combobox with checkbox

When user checks on the “All” item, the other checkboxes should be checked as well. I am using the codes below but it doesn’t work.

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;
      }
 }
user1850936
  • 129
  • 3
  • 18

2 Answers2

1

use CompositeCollection inside your ComboBox, check below answers for more information

Community
  • 1
  • 1
Damith
  • 62,401
  • 13
  • 102
  • 153
1

There are few issues in your code let me first tell you about those issue.

  1. your if condition for identifying the "Select All" checkbox is incorrect. Your need to use Contains() instead of equals()
  2. The checkbox you are fetching is not the correct one within the comboBox Item. If you try to see the checkBox.Content property you will see null as result.

See below code to select all the checkboxes within the comboBox when "Select All" checkbox is checked.

Your Checkbox Click event should be as below.

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

I have added a new method to fetch the visual child within any element from the name of the child and its type.

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;
        }
jadavparesh06
  • 926
  • 7
  • 11
  • thanks! your codes work perfectly for me. I am stuck at the visualtreehelper before. thanks for guiding me to the right way – user1850936 Oct 21 '14 at 05:33
  • 1
    It will also work for a comboBox having more items to display a vertical scrollbar. just verify this code for comboBox having enough items to display a vertical scrollbar in pop-up – jadavparesh06 Oct 21 '14 at 05:39
  • 1
    well, I've just tried it. It does work when there is a vertical scrollbar. I thought the `ItemsPanel` of ComboBox used `VirtualizingStackPanel`, in that case it would not work. – King King Oct 21 '14 at 06:11
  • 1
    @KingKing thanks for your advice too. I will take care of it if I use VirtualizingStackPanel next time. – user1850936 Oct 21 '14 at 07:05
  • 1
    @user1850936 in case the `VirtualizingStackPanel` is used, you can set it off by setting the attached property `VirtualizingPanel.IsVirtualizing` to `false`. Normally a combobox does not contain many items, so that's why `VirtualizingStackPanel` is not used by default. – King King Oct 21 '14 at 07:08