DCSIMG
WPF: ComboBox with CheckBoxes as items (it will even update on the fly!) - justguy's
Sign in | Join | Help

justguy's

Thoughts, issues and practice: life, liberty, SharePoint and .NET.

WPF: ComboBox with CheckBoxes as items (it will even update on the fly!)

Hi,

 

Recently I’ve come across something weird… I needed a ComboBox that will allow the user to select multiple items.
The the solution coming to mind is using CheckBoxes. I have found several examples, but neither one displayed the selected items with pretty commas (like this: image ).

 

I’ve decided the best solution willl be taking an example from MSDN and modifying it to suite my needs.

Steps: (actually took ALOT longer and was ALOT harder – learning curves and such)

  1. Created a UserControl.
  2. Added the ComboBox from the MSDN sample.
  3. Created 3 dependency properties:
    • Text – retrieves the text of the selected items
    • ItemsSource – the items to display (currently bound to Title and IsSelected)
    • DefaultText – the text to display if no items were checked
  4. Bound the ItemsSource properties of the UserControl and the ComboBox.
  5. Added a Click event to the CheckBox that refreshes the text field in the ContentPresenter.

Usage

<Window





xmlns:controls="clr-namespace:Controls;assembly=Controls"
<controls:ComboWithCheckboxes
    x:Name="cbLanguages"
    Height="22"
    DefaultText="Choose Subtitles..."
    ItemsSource="{Binding}"
    />

 

Result

image

image

image

Code

 

ComboWithCheckboxes.xaml

<UserControl x:Class="Controls.ComboWithCheckboxes"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Height="22" Width="120"
    x:Name="UserControl"
    >
    
    <UserControl.Resources>
        <LinearGradientBrush x:Key="NormalBrush" StartPoint="0,0" EndPoint="0,1">
            <GradientBrush.GradientStops>
                <GradientStopCollection>
                    <GradientStop Color="#FFF" Offset="0.0"/>
                    <GradientStop Color="#CCC" Offset="1.0"/>
                </GradientStopCollection>
            </GradientBrush.GradientStops>
        </LinearGradientBrush>

        <LinearGradientBrush x:Key="NormalBorderBrush" StartPoint="0,0" EndPoint="0,1">
            <GradientBrush.GradientStops>
                <GradientStopCollection>
                    <GradientStop Color="#CCC" Offset="0.0"/>
                    <GradientStop Color="#444" Offset="1.0"/>
                </GradientStopCollection>
            </GradientBrush.GradientStops>
        </LinearGradientBrush>

        <SolidColorBrush x:Key="GlyphBrush" Color="#444" />

        <LinearGradientBrush x:Key="DarkBrush" StartPoint="0,0" EndPoint="0,1">
            <GradientBrush.GradientStops>
                <GradientStopCollection>
                    <GradientStop Color="#FFF" Offset="0.0"/>
                    <GradientStop Color="#AAA" Offset="1.0"/>
                </GradientStopCollection>
            </GradientBrush.GradientStops>
        </LinearGradientBrush>

        <LinearGradientBrush x:Key="PressedBrush" StartPoint="0,0" EndPoint="0,1">
            <GradientBrush.GradientStops>
                <GradientStopCollection>
                    <GradientStop Color="#BBB" Offset="0.0"/>
                    <GradientStop Color="#EEE" Offset="0.1"/>
                    <GradientStop Color="#EEE" Offset="0.9"/>
                    <GradientStop Color="#FFF" Offset="1.0"/>
                </GradientStopCollection>
            </GradientBrush.GradientStops>
        </LinearGradientBrush>

        <SolidColorBrush x:Key="DisabledForegroundBrush" Color="#888" />

        <SolidColorBrush x:Key="DisabledBackgroundBrush" Color="#EEE" />

        <SolidColorBrush x:Key="WindowBackgroundBrush" Color="#FFF" />

        <SolidColorBrush x:Key="SelectedBackgroundBrush" Color="#DDD" />

        <ControlTemplate x:Key="ComboBoxToggleButton" TargetType="ToggleButton">
            <Grid>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition />
                    <ColumnDefinition Width="20" />
                </Grid.ColumnDefinitions>
                <Border
                  x:Name="Border" 
                  Grid.ColumnSpan="2"
                  CornerRadius="2"
                  Background="{StaticResource NormalBrush}"
                  BorderBrush="{StaticResource NormalBorderBrush}"
                  BorderThickness="1" />
                <Border 
                  Grid.Column="0"
                  CornerRadius="2,0,0,2" 
                  Margin="1" 
                  Background="{StaticResource WindowBackgroundBrush}" 
                  BorderBrush="{StaticResource NormalBorderBrush}"
                  BorderThickness="0,0,1,0" />
                <Path 
                  x:Name="Arrow"
                  Grid.Column="1"     
                  Fill="{StaticResource GlyphBrush}"
                  HorizontalAlignment="Center"
                  VerticalAlignment="Center"
                  Data="M 0 0 L 4 4 L 8 0 Z"/>
            </Grid>
            <ControlTemplate.Triggers>
                <Trigger Property="ToggleButton.IsMouseOver" Value="true">
                    <Setter TargetName="Border" Property="Background" Value="{StaticResource DarkBrush}" />
                </Trigger>
                <Trigger Property="ToggleButton.IsChecked" Value="true">
                    <Setter TargetName="Border" Property="Background" Value="{StaticResource PressedBrush}" />
                </Trigger>
                <Trigger Property="IsEnabled" Value="False">
                    <Setter TargetName="Border" Property="Background" Value="{StaticResource DisabledBackgroundBrush}" />
                    <Setter TargetName="Border" Property="BorderBrush" Value="{StaticResource DisabledBorderBrush}" />
                    <Setter Property="Foreground" Value="{StaticResource DisabledForegroundBrush}"/>
                    <Setter TargetName="Arrow" Property="Fill" Value="{StaticResource DisabledForegroundBrush}" />
                </Trigger>
            </ControlTemplate.Triggers>
        </ControlTemplate>
        
        <ControlTemplate x:Key="ComboBoxTextBox" TargetType="TextBox">
            <Border x:Name="PART_ContentHost" Focusable="False" Background="{TemplateBinding Background}" />
        </ControlTemplate>

        <Style x:Key="{x:Type ComboBoxItem}" TargetType="ComboBoxItem">
            <Setter Property="SnapsToDevicePixels" Value="true"/>
            <Setter Property="OverridesDefaultStyle" Value="true"/>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="ComboBoxItem">
                        <Border 
                          Name="Border"
                          Padding="2"
                          SnapsToDevicePixels="true">
                            <ContentPresenter />
                        </Border>
                        <ControlTemplate.Triggers>
                            <Trigger Property="IsHighlighted" Value="true">
                                <Setter TargetName="Border" Property="Background" Value="{StaticResource SelectedBackgroundBrush}"/>
                            </Trigger>
                            <Trigger Property="IsEnabled" Value="false">
                                <Setter Property="Foreground" Value="{StaticResource DisabledForegroundBrush}"/>
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </UserControl.Resources>

    <ComboBox
        x:Name="CheckableCombo"
        SnapsToDevicePixels="True"
        OverridesDefaultStyle="True"
        ScrollViewer.HorizontalScrollBarVisibility="Auto"
        ScrollViewer.VerticalScrollBarVisibility="Auto"
        ScrollViewer.CanContentScroll="True"
        IsSynchronizedWithCurrentItem="True"
        MinWidth="120"
        MinHeight="20"
        ItemsSource="{Binding ElementName=UserControl, Path=ItemsSource}"
        DataContext="{Binding ElementName=UserControl, Path=DataContext}"
        >
        <ComboBox.ItemTemplate>
            <HierarchicalDataTemplate>
                <CheckBox Content="{Binding Title}"
                          IsChecked="{Binding Path=IsSelected, Mode=TwoWay}"
                          Tag="{RelativeSource FindAncestor, AncestorType={x:Type ComboBox}}"
                          Click="CheckBox_Click"
                          />
            </HierarchicalDataTemplate>
        </ComboBox.ItemTemplate>

        <ComboBox.Template>
            <ControlTemplate TargetType="ComboBox">
                <Grid>
                    <ToggleButton 
                        Name="ToggleButton" 
                        Template="{StaticResource ComboBoxToggleButton}" 
                        Grid.Column="2" 
                        Focusable="false"
                        IsChecked="{Binding Path=IsDropDownOpen,Mode=TwoWay,RelativeSource={RelativeSource TemplatedParent}}"
                        ClickMode="Press">
                    </ToggleButton>
                    <ContentPresenter
                        x:Name="Presenter"
                        IsHitTestVisible="False" 
                        Margin="3,3,23,3"
                        VerticalAlignment="Center"
                        HorizontalAlignment="Left">
                        <ContentPresenter.Content>
                            <TextBlock TextTrimming="CharacterEllipsis"
                                       Text="{Binding Path=Text,Mode=TwoWay,RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=UserControl}}" />
                        </ContentPresenter.Content>
                    </ContentPresenter>
                    <!-- Content="{TemplateBinding SelectionBoxItem}" -->
                    <TextBox x:Name="EditableTextBox"
                        Style="{x:Null}" 
                        Template="{StaticResource ComboBoxTextBox}" 
                        HorizontalAlignment="Left" 
                        VerticalAlignment="Center" 
                        Margin="3,3,23,3"
                        Focusable="True" 
                        Background="Transparent"
                        Visibility="Hidden"
                        IsReadOnly="{TemplateBinding IsReadOnly}"/>
                    <Popup 
                        Name="Popup"
                        Placement="Bottom"
                        IsOpen="{TemplateBinding IsDropDownOpen}"
                        AllowsTransparency="True" 
                        Focusable="False"
                        PopupAnimation="Slide">
                        <Grid 
                                  Name="DropDown"
                                  SnapsToDevicePixels="True"                
                                  MinWidth="{TemplateBinding ActualWidth}"
                                  MaxHeight="{TemplateBinding MaxDropDownHeight}">
                            <Border 
                                    x:Name="DropDownBorder"
                                    Background="{StaticResource WindowBackgroundBrush}"
                                    BorderThickness="1"
                                    BorderBrush="{StaticResource SolidBorderBrush}"/>
                            <ScrollViewer Margin="4,6,4,6" SnapsToDevicePixels="True" DataContext="{Binding}">
                                <StackPanel IsItemsHost="True" KeyboardNavigation.DirectionalNavigation="Contained" />
                            </ScrollViewer>
                        </Grid>
                    </Popup>
                </Grid>
                <ControlTemplate.Triggers>
                    <Trigger Property="HasItems" Value="false">
                        <Setter TargetName="DropDownBorder" Property="MinHeight" Value="95"/>
                    </Trigger>
                    <Trigger Property="IsEnabled" Value="false">
                        <Setter Property="Foreground" Value="{StaticResource DisabledForegroundBrush}"/>
                    </Trigger>
                    <Trigger Property="IsGrouping" Value="true">
                        <Setter Property="ScrollViewer.CanContentScroll" Value="false"/>
                    </Trigger>
                    <Trigger SourceName="Popup" Property="Popup.AllowsTransparency" Value="true">
                        <Setter TargetName="DropDownBorder" Property="CornerRadius" Value="4"/>
                        <Setter TargetName="DropDownBorder" Property="Margin" Value="0,2,0,0"/>
                    </Trigger>
                    <Trigger Property="IsEditable"
                   Value="true">
                        <Setter Property="IsTabStop" Value="false"/>
                        <Setter TargetName="EditableTextBox" Property="Visibility"    Value="Visible"/>
                        <Setter TargetName="Presenter" Property="Visibility" Value="Hidden"/>
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </ComboBox.Template>
    </ComboBox>
</UserControl>

 

ComboWithCheckboxes.xaml.cs

usingSystem.Windows;

namespace Controls
{
    /// <summary>
    ///
Interaction logic for ComboWithCheckboxes.xaml
  
/// </summary>
  
public partial classComboWithCheckboxes
  
{
        #regionDependency Properties
        /// <summary>
        ///
Gets or sets a collection used to generate the content of the ComboBox
      
/// </summary>
      
public objectItemsSource
        {
            get{ return(object)GetValue(ItemsSourceProperty); }
            set
          
{
                SetValue(ItemsSourceProperty, value);

                SetText();
            }
        }

        public static readonlyDependencyPropertyItemsSourceProperty =
            DependencyProperty.Register("ItemsSource", typeof(object), typeof(ComboWithCheckboxes), newUIPropertyMetadata(null));

        /// <summary>
        ///
Gets or sets the text displayed in the ComboBox
      
/// </summary>
      
public stringText
        {
            get{ return(string)GetValue(TextProperty); }
            set{ SetValue(TextProperty, value); }
        }

        public static readonlyDependencyPropertyTextProperty =
            DependencyProperty.Register("Text", typeof(string), typeof(ComboWithCheckboxes), newUIPropertyMetadata(string.Empty));


        /// <summary>
        ///
Gets or sets the text displayed in the ComboBox if there are no selected items
      
/// </summary>
      
public stringDefaultText
        {
            get{ return(string)GetValue(DefaultTextProperty); }
            set{ SetValue(DefaultTextProperty, value); }
        }

        // Using a DependencyProperty as the backing store for DefaultText.  This enables animation, styling, binding, etc...
      
public static readonlyDependencyPropertyDefaultTextProperty =
            DependencyProperty.Register("DefaultText", typeof(string), typeof(ComboWithCheckboxes), newUIPropertyMetadata(string.Empty));
        #endregion


        public
ComboWithCheckboxes()
        {
            InitializeComponent();
        }

        /// <summary>
        ///
Whenever a CheckBox is checked, change the text displayed
      
/// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
      
private voidCheckBox_Click(objectsender, RoutedEventArgse)
        {
            SetText();
        }

        /// <summary>
        ///
Set the text property of this control (bound to the ContentPresenter of the ComboBox)
      
/// </summary>
      
private voidSetText()
        {
            this.Text = (this.ItemsSource != null) ?
                this.ItemsSource.ToString() : this.DefaultText;

            // set DefaultText if nothing else selected
          
if(string.IsNullOrEmpty(this.Text))
            {
                this.Text = this.DefaultText;
            }
        }
    }
}

That’s it!

Adi.

Comments List

# re: WPF: ComboBox with CheckBoxes as items (it will even update on the fly!)

Published at Wednesday, March 11, 2009 8:44 AM by Masoom Ali  

hi.. that was good article please send me the control on from to email address "im.masoomali@gmail.com"

# re: WPF: ComboBox with CheckBoxes as items (it will even update on the fly!)

Published at Monday, March 16, 2009 10:46 AM by Mar  

Hi. This article is great, i need to do the exact same thing. Could you please send me the control via email or upload it here? I tried to modify the code in here, but i´m new at this and i have errors that i don´t know how to correct.

Help me please!!

My email address is: mmgv5@yahoo.com

Thanks!!

# re: WPF: ComboBox with CheckBoxes as items (it will even update on the fly!)

Published at Friday, April 10, 2009 8:01 PM by Gajender Pal  

what ItemSource i can provide to this control?

# re: WPF: ComboBox with CheckBoxes as items (it will even update on the fly!)

Published at Thursday, April 23, 2009 10:59 PM by Rob Lewis  

Adi, this is so good it's not even funny.  Thank you!!

Gajender - This worked for me:

   public class Node

   {

       public Node(string n) { Title = n; }

       public string Title { get; set; }

       public bool IsSelected { get; set; }

   }

   public class ObservableNodeList : ObservableCollection<Node>

   {

       public ObservableNodeList()

       {

       }

       public override string  ToString()

       {

           StringBuilder outString = new StringBuilder();

           foreach (Node s in this.Items)

           {

               if (s.IsSelected == true)

               {

                   outString.Append(s.Title);

                   outString.Append(',');

               }

           }

           return outString.ToString().TrimEnd(new char[] {','});

       }

   }

# re: WPF: ComboBox with CheckBoxes as items (it will even update on the fly!)

Published at Friday, April 24, 2009 1:52 AM by Gavin McKay  

Fantastic work Adi, this is an extremely useful control!  Just solved my multi-selection headache.

Rob - side note, the using statements required for your nodelist are:

using System.Collections.Generic;

using System.Collections.ObjectModel;

using System.Text;

I got compile errors because I was missing .ObjectModel.

# re: WPF: ComboBox with CheckBoxes as items (it will even update on the fly!)

Published at Wednesday, May 20, 2009 6:08 AM by justguy  

Hi, guys,

Sorry for the late response. The web's been keeping me busy...

Glad to see someone found a use for this control.

I hope Gavin's comment helped with the errors.

# re: WPF: ComboBox with CheckBoxes as items (it will even update on the fly!)

Published at Monday, June 01, 2009 5:56 PM by Amy Pham  

This is great! You saved me ;-)  I liked this post so much that I revisited other posts discussing about multiselect combobox to come to this post. Thank you, thank you Adi!

# re: WPF: ComboBox with CheckBoxes as items (it will even update on the fly!)

Published at Tuesday, June 02, 2009 6:15 PM by Amy  

Thank you for posting this code Adi.  Would you please give some ideas on how to use this  multiselect combobox in a datagrid? My users want to have multiselect columns in a datagrid.

Thanks,

Amy

# re: WPF: ComboBox with CheckBoxes as items (it will even update on the fly!)

Published at Wednesday, June 10, 2009 1:19 AM by Sid  

Thx for the code. However that does not work for me:

1) I imported the code in a separate project and referenced from my VB.Net project.

2) I created a class that contains checkboxes data (the Node class of the Rob Lewis comment)

3) Then I created a collection with an overrided ToString method to contain the nodes collection.

4) Setting the ItemsSource to that populated collection binds everything well, but the data:

The dropdown will populate the popup and show the text ("Checkbox A, Checkbox B, ...").

The popup will show the correct number of checkboxes, but will not bind to the Node class: checkboxes will not be checked and the Text property will not be set. Neither the object>source binding will work: checking the checkbox will not change the IsSelected property of my Node.

Yes, the Node class has IsSelected and Title fields.

May you post or send an example?

E-Mail: alberto AT alvy DOT tv

Thx in advance!

# re: WPF: ComboBox with CheckBoxes as items (it will even update on the fly!)

Published at Thursday, June 11, 2009 1:00 PM by mareng  

Great code!

I use it in a wpf toolkit datagrid, and it almost works perfectly. But when a row is selected, the usercontrol dosen´t display the default text or if any values is selected, them as a comma seperated string, it displays nothing, otherwise it works fine.

Any suggestions?

Thanks in advance!

# re: WPF: ComboBox with CheckBoxes as items (it will even update on the fly!)

Published at Sunday, June 14, 2009 8:53 AM by justguy  

Hi, Alberto,

I'm sorry, but my laptop dropped dead, and I don't have an example to provide.

Can anyone else help?

# re: WPF: ComboBox with CheckBoxes as items (it will even update on the fly!)

Published at Tuesday, June 30, 2009 12:01 PM by ronK  

Great example thought this is not working for me and I can't understand why.

I have copied the user control with the .cs, and created the node and the collection classes.

I populate the item source in the code behind :

(the button.YParametersFieldNames holds the previously selected parameters)

ObservableNodeList itemSource = new ObservableNodeList();

               List<string> selectedParameters = new List<string>(button.YParametersFieldNames);

               foreach (DataColumn column in button.DataSource.Columns)

               {

                   Node item = new Node(column.Caption);

                   item.IsSelected = selectedParameters.Contains(item.Title);

                   itemSource.Add(item);

               }

               comboYParameters.ItemsSource = itemSource;

and the result if the comboBox looks empty with no available items to select from.

what am I missing here ?

# re: WPF: ComboBox with CheckBoxes as items (it will even update on the fly!)

Published at Tuesday, June 30, 2009 3:38 PM by ronK  

forget my last comment, found the erorr - I accidently removed the x:name of the user control which made the binding of the item source to not work correctly.

# re: WPF: ComboBox with CheckBoxes as items (it will even update on the fly!)

Published at Tuesday, June 30, 2009 10:56 PM by smitha  

Hi,

can you please post the code that goes in window.xaml.cs ,I am a newbie trying to use usercontrols and i am unable to get your code working,

Thanks you so much

# re: WPF: ComboBox with CheckBoxes as items (it will even update on the fly!)

Published at Tuesday, June 30, 2009 11:00 PM by Smitha  

Hi all,

those who got it to work.

can you plesae see what is wrong in my code.

This is how my window.xaml.cs looks.Rest of teh code is directly from the code above.I cannot get the checkboxes to display.

public partial class Window1 : Window

   {

       public Window1()

       {

           InitializeComponent();

           ObservableNodeList itemSource = new ObservableNodeList();

           itemSource.Add(new Node("Willa"));

           itemSource.Add(new Node("Isak"));

           itemSource.Add(new Node("Victor"));

           itemSource.Add(new Node("Jules"));

           cbLanguages.ItemsSource = itemSource;

           cbLanguages.DataContext = itemSource;

         //  listBox1.ItemsSource = itemSource;

       }

   }

      public class Node

   {

          public Node(string n) { Title = n; }

       public string Title { get; set; }

       public bool IsSelected { get; set; }

   }

   public class ObservableNodeList : ObservableCollection<Node>

   {

       public ObservableNodeList()

       {

       }

       public override string ToString()

       {

           StringBuilder outString = new StringBuilder();

           foreach (Node s in this.Items)

           {

               if (s.IsSelected == true)

               {

                   outString.Append(s.Title);

                   outString.Append(',');

               }

           }

           return outString.ToString().TrimEnd(new char[] { ',' });

       }

   }

}

# re: WPF: ComboBox with CheckBoxes as items (it will even update on the fly!)

Published at Tuesday, June 30, 2009 11:09 PM by Smitha  

Cant see my two posts

# re: WPF: ComboBox with CheckBoxes as items (it will even update on the fly!)

Published at Tuesday, July 07, 2009 2:33 PM by Ranga  

Can you please provide, proper sample.

# re: WPF: ComboBox with CheckBoxes as items (it will even update on the fly!)

Published at Tuesday, July 07, 2009 4:24 PM by Ranga  

Steps to make it work:

1. Update the property "ItemsSource" in ComboWithCheckboxes class with the following code.

public object ItemsSource

       {

           get { return (object)GetValue(ItemsSourceProperty); }

           set

           {

               this.CheckableCombo.ItemsSource = (System.Collections.IEnumerable)value;

               SetValue(ItemsSourceProperty, value);

               SetText();

           }

       }

2. Add the following code in the window1.xaml.cs

ObservableNodeList itemSource = new ObservableNodeList();

           Node a = new Node("Dog");

           a.IsSelected = true;

           itemSource.Add(a);

           Node b = new Node("Monkey");

           b.IsSelected = false;

           itemSource.Add(b);

           cbLanguages.ItemsSource = itemSource;

# re: WPF: ComboBox with CheckBoxes as items (it will even update on the fly!)

Published at Friday, September 18, 2009 12:21 PM by Max  

Hi, thank you for the control.

But I can't find the DisabledBorderBrush resource... can you add it or point to the location?

# re: WPF: ComboBox with CheckBoxes as items (it will even update on the fly!)

Published at Wednesday, November 04, 2009 5:03 PM by KK  

Hi Folks,

Can anyone of you who got it to work can please email me the usercontrol.

Will appreciate it. Thanks a lot!!!

my email id is mkkeerthi@rediffmail.com

# re: WPF: ComboBox with CheckBoxes as items (it will even update on the fly!)

Published at Monday, November 16, 2009 2:08 PM by steven hawkes  

I am having a problem getting the ItemSource events to fire.

For example, I databind the ItemsSource property to an ObservableNodeList as a public property.   However, when I write my data into this property, the data is written into the ItemsSource and is available in the drop down but the SetText is not called so the selected items in the list are not updated in the ComboBox view when collapsed.

If I subsequently open the combopox and deselect an item , then the combobox refreshes from the ButtonClick event.

I checked and for some reason the Set and Get methods are not being called at all when I assign to the ItemSource property.

any ideas what I could be doing wrong?    

# re: WPF: ComboBox with CheckBoxes as items (it will even update on the fly!)

Published at Saturday, January 16, 2010 4:03 PM by Vladimir  

Hello,

Great control, I like it. But I've got some problems with stretching it horizontally. It doesn't behave like usual combobox - setting HorizontalAlignment isn't enough for stretching it. Removing Width="120" setting of UserControl also dodn't help me. Could somebody help me?

The task is to show this control stretched horizontally.

# re: WPF: ComboBox with CheckBoxes as items (it will even update on the fly!)

Published at Saturday, January 16, 2010 4:50 PM by Vladimir  

Never mind, please. The problem was in code around the control, not in control itself.

# re: WPF: ComboBox with CheckBoxes as items (it will even update on the fly!)

Published at Monday, January 18, 2010 6:55 PM by Reto  

Is it possible to download the latest version of the control from somewhere? Or could somebody mail me this to

"rgb64(at)bluemail.ch"

Thanks

# re: WPF: ComboBox with CheckBoxes as items (it will even update on the fly!)

Published at Monday, January 25, 2010 10:44 AM by Mikael Madsen  

Hey there. I'm having some problems with GetValue and setValue in the codebehind file. can anyone tell me how to correct these errors or maybe send a working sourcefile to me at:

Mikael_Madkasse@Hotmail.com

kthxbye

# re: WPF: ComboBox with CheckBoxes as items (it will even update on the fly!)

Published at Friday, January 29, 2010 9:21 PM by Bahri Gungor  

You can resolve the GetValue/SetValue compile errors by deriving the control (in the codebehind) from UserControl.

  public partial class ComboWithCheckboxes : UserControl

# re: WPF: ComboBox with CheckBoxes as items (it will even update on the fly!)

Published at Wednesday, February 10, 2010 8:10 PM by Satyam Kumar  

Hi Adi,

It is really a beautifull code. Can you please send me the code to me subham11@gmail.com

Thanks & Regards,

Satyam Kumar.

# re: WPF: ComboBox with CheckBoxes as items (it will even update on the fly!)

Published at Thursday, March 04, 2010 10:30 PM by Joe  

You can use generics to make multi-select entity collections easier:

1) Create interface "ISelectable":

public interface ISelectable

{

   bool IsSelected { get; set; }

   string DisplayName { get; }

}

2) Implement ISelectable on entity:

public partial MyEntity : ISelectable

{

   public bool IsSelected { get; set; }

   public string DisplayName {

       get { return this.PropertyToDisplay; }

   }

}

3) Create MultiSelectEntityCollection<T>:

public class MultiSelectEntityCollection<T>

   : ObservableCollection<T>

   where T : ISelectable

{

   public override string ToString()

   {

       var str = new StringBuilder();

       Items.ToList().ForEach( item =>

       {

           if ( item.IsSelected )

           {

               str.Append( item.DisplayName );

               str.Append( ", " );

           }

       } );

       return str.ToString().TrimEnd( ',' );

   }

}

4) Use MultiSelectEntityCollection in your code:

public MultiSelectEntityCollection<MyEntity> MyMultiSelectableEntityCollection { get; set; }

Cheers,

refereejoe at yahoo dot com

# re: WPF: ComboBox with CheckBoxes as items (it will even update on the fly!)

Published at Monday, March 22, 2010 1:42 PM by king  

Implementing this user control in MVVM pattern, itemsource doesn't change when we change this from code

# re: WPF: ComboBox with CheckBoxes as items (it will even update on the fly!)

Published at Thursday, March 25, 2010 10:35 AM by frank  

ItemsSource doesn't work initially in a Datagrid (wpftoolkit).

This is a workaround. In the grid:

<dg:DataGridTemplateColumn Header="Include">

  <dg:DataGridTemplateColumn.CellTemplate>

      <DataTemplate>

         <common:ComboWithCheckboxes Text="{Binding Path=DimensionValues, Mode=OneTime, Converter={StaticResource initialTextConverter}}" ItemsSource="{Binding DimensionValues}" DefaultText="None" MaxWidth="380" AllCheckedText="All" />

      </DataTemplate>

  </dg:DataGridTemplateColumn.CellTemplate>

</dg:DataGridTemplateColumn>

The Converter:

    [ValueConversion(typeof(object), typeof(string))]

   public class InitialTextConverter : IValueConverter

   {

       public object Convert(object value, Type targetType,

           object parameter, System.Globalization.CultureInfo culture)

       {

           if (value is ObservableNodeList)

           {

               ObservableNodeList list = (ObservableNodeList)value;

               if (list.AllChecked())

                   return "All";

               if (list.NoneChecked())

                   return "None";

               return list.ToString();

           }

           return string.Empty;

       }

       public object ConvertBack(object value, Type targetType,

           object parameter, System.Globalization.CultureInfo culture)

       {

           // we don't intend this to ever be called

           return null;

       }

   }

I also had to change the content of the default Combobox (as the text was not visible sometimes):

<ContentPresenter.Content>

  <TextBlock TextTrimming="CharacterEllipsis"

       Foreground="{DynamicResource {x:Static SystemColors.WindowTextBrushKey}}"

       Text="{Binding Path=Text,Mode=OneWay,RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=UserControl}}" />

</ContentPresenter.Content>

# re: WPF: ComboBox with CheckBoxes as items (it will even update on the fly!)

Published at Friday, May 07, 2010 1:57 PM by guruvn  

So far so good, I have tried to figure out how to get it done via using xaml only, but sound seems to be we have to handle it on both xaml & code behind

# re: WPF: ComboBox with CheckBoxes as items (it will even update on the fly!)

Published at Friday, July 09, 2010 6:47 PM by Andreas  

This example works great! Thanks for that!

There is only a small "bug" I have with the xaml:

When I select some items, sometimes I click "between" two items (meaning: somewhere in the top or bottom of an item). The instead of selecting this item, the selection box disappears. Does anyone have a solution for that?

# re: WPF: ComboBox with CheckBoxes as items (it will even update on the fly!)

Published at Friday, July 16, 2010 10:31 AM by Debasish das  

Please send me the wpf control in mailid as soon as possible.

my mail id:-

debasish.das04@gmail.com

# re: WPF: ComboBox with CheckBoxes as items (it will even update on the fly!)

Published at Friday, July 16, 2010 2:58 PM by Debasish das  

Please send me the control with code...my mail id:- debasish.das04@gmail.com

# re: WPF: ComboBox with CheckBoxes as items (it will even update on the fly!)

Published at Friday, July 30, 2010 11:54 AM by Andreas  

If found a solution to the follwing issuee posted a little bit earlier by me:

The problem was when I selected some items, sometimes when clicking "between" two items (meaning: somewhere in the top or bottom of an item), instead of selecting this item the selection box was closed. Apparently the check box did not register the click, and somehow the event was registered as clicking outside of the popup.

There is an easy way to solve the problem: Set the margin of the checkbox in the <ComboBox.ItemTemplate> to negative values!

So, e.g. Margin="0,-5,0,-5" added to the checkbox tag would resolve the issue.

# re: WPF: ComboBox with CheckBoxes as items (it will even update on the fly!)

Published at Monday, August 02, 2010 11:21 AM by Pritesh  

Hi.

Just went through your code.

It is the same type of control I wanted to implemented.

But somehow the code is not working.

Can you please share the sourcecode once again?

# re: WPF: ComboBox with CheckBoxes as items (it will even update on the fly!)

Published at Sunday, August 08, 2010 12:43 AM by Guru s  

I am able to fix the code and make it run. I want to using this control in MVVM model. I am not able to get the selected checkbox value. Does any one tryit. If yes please post the code.

Thanks,

# re: WPF: ComboBox with CheckBoxes as items (it will even update on the fly!)

Published at Friday, September 10, 2010 4:28 PM by Prad  

How can I add

1. tooltip (TextBox) and

2. quick search (like on key press of D should highlight items starting with D)

to the above example?

# re: WPF: ComboBox with CheckBoxes as items (it will even update on the fly!)

Published at Friday, September 10, 2010 11:03 PM by Prad  

I have been trying to clear all the checkbox for a button Clear All.

The TextProperty works but the checkbox are still in check state although ObservabelNodeList shows isSelected = false.

private void button1_Click(object sender, RoutedEventArgs e)

       {

           foreach (Node n in (ObservableNodeList)comboCheckBox1.ItemsSource)

           {

               if (n.IsSelected) n.IsSelected = false;

           }

       }      

# re: WPF: ComboBox with CheckBoxes as items (it will even update on the fly!)

Published at Wednesday, October 06, 2010 9:04 PM by venkat  

Hi I like your solution, can you email me the complete solution. My email is venkatap@gmail.com

# re: WPF: ComboBox with CheckBoxes as items (it will even update on the fly!)

Published at Monday, October 11, 2010 4:52 AM by Amy Tusun  

Hi, I will be able to use your solution in my project if I can make the control editable (i.e. user can type as well as check boxes). In other words I need IsEditable property to be true. I tried to add this property but it didn't work.

# re: WPF: ComboBox with CheckBoxes as items (it will even update on the fly!)

Published at Saturday, November 06, 2010 11:39 PM by Vilas  

Solution looks elegant. Can you please email me the solution at vilash001@yahoo.com. Thanks again

# re: WPF: ComboBox with CheckBoxes as items (it will even update on the fly!)

Published at Tuesday, November 09, 2010 11:55 AM by soon  

Can you please email me the solution at cpjack@yahoo.cn. Thanks again

# re: WPF: ComboBox with CheckBoxes as items (it will even update on the fly!)

Published at Thursday, November 11, 2010 5:34 AM by sandeep  

This is what i wanted. Other 3rd party ones doesn't work for my scenarios. Please email me the solution at deshmukhsandeep023@gmail.com.

# re: WPF: ComboBox with CheckBoxes as items (it will even update on the fly!)

Published at Friday, November 12, 2010 12:56 PM by Gabe  

I want to implement it, but a lot of errors appeared.

So, can someone send me the source codes? Please help me.

Thanks.

<gabe@logiciel.com>

# re: WPF: ComboBox with CheckBoxes as items (it will even update on the fly!)

Published at Monday, November 22, 2010 8:40 PM by guissi  

ras

# re: WPF: ComboBox with CheckBoxes as items (it will even update on the fly!)

Published at Monday, November 29, 2010 8:55 AM by minho  

Please send me the control with code...my mail id: sexyminho@hotmail.com

# re: WPF: ComboBox with CheckBoxes as items (it will even update on the fly!)

Published at Friday, December 10, 2010 3:29 PM by mochy  

Hey,

Just wanted to say thank you. I got it working easily, this solution is perfect for my application.

I implemented it using MVVM pattern and I am having an issue when switching tabs, the Text property becomes empty again, even tough things are checked in the list... have to figure this out!

Thanks again!

# re: WPF: ComboBox with CheckBoxes as items (it will even update on the fly!)

Published at Wednesday, March 02, 2011 4:12 PM by Matthew  

Nice, but my problem with this is that if you click *between* an item while the box is dropped-down, the box closes.  This, to me, renders the control unusable.  Is there any way to prevent this?

# re: WPF: ComboBox with CheckBoxes as items (it will even update on the fly!)

Published at Wednesday, March 02, 2011 6:54 PM by Morgan  

Getting this error in my project

System.Windows.Data Error: 8 : Cannot save value from target back to source. BindingExpression:Path=IsDropDownOpen; DataItem='ComboBox' (Name='CheckableCombo'); target element is 'ToggleButton' (Name='ToggleButton'); target property is 'IsChecked' (type 'Nullable`1') XamlParseException:'System.Windows.Markup.XamlParseException: Provide value on 'System.Windows.Markup.StaticResourceHolder' threw an exception. ---> System.Exception: Cannot find resource named 'ValidationToolTipTemplate'. Resource names are case sensitive.

  at System.Windows.StaticResourceExtension.ProvideValue(IServiceProvider serviceProvider

I do indeed have the template resource and it is in my project.  Not sure where else to find help for this... It errors out with a Object not set to an instance..

# re: WPF: ComboBox with CheckBoxes as items (it will even update on the fly!)

Published at Wednesday, March 16, 2011 11:33 AM by sunnyc  

Would u please send a example for me?

sunny29301@163.com,I have some problem with the data binding,It shows nothing.Thank u very much

# re: WPF: ComboBox with CheckBoxes as items (it will even update on the fly!)

Published at Friday, March 25, 2011 1:39 PM by Ryan  

Please send me the code mrachek@gmail.com

# re: WPF: ComboBox with CheckBoxes as items (it will even update on the fly!)

Published at Wednesday, March 30, 2011 6:21 PM by Ken  

To fix the problem Andreas and Mathew talk about remove the Padding=2 from the Border in the ComboBoxItem Style

# re: WPF: ComboBox with CheckBoxes as items (it will even update on the fly!)

Published at Friday, April 01, 2011 7:32 PM by Matthew  

Thanks for providing the "Padding=2" removal suggestion, and while it does indeed work, it jams the checkboxes up so that there's no space between them!  It's a great control, but shame I'm still a WPF newbie and am not able to solve things such as this for myself!

# re: WPF: ComboBox with CheckBoxes as items (it will even update on the fly!)

Published at Tuesday, April 19, 2011 11:19 AM by Sameer  

Hi Adi, Can u plz send me the code of above control. i am in need of the same. plz help me.................

# re: WPF: ComboBox with CheckBoxes as items (it will even update on the fly!)

Published at Monday, May 09, 2011 4:45 PM by Roshil  

Pls send me the code.

roshilk@gmail.com

# re: WPF: ComboBox with CheckBoxes as items (it will even update on the fly!)

Published at Friday, May 13, 2011 1:02 AM by Gunnar  

Hi Adi,

Code works a charm after a bit of fiddling.

Most appreciated. Saved me some time and learned interesting stuff.

Thanks mate.

# re: WPF: ComboBox with CheckBoxes as items (it will even update on the fly!)

Published at Friday, May 13, 2011 9:35 PM by Ankur Juneja  

am in urgent need of this control. please send me at the given email id. please help me out guys....

ankur_juneja2004@yahoo.co.in

# re: WPF: ComboBox with CheckBoxes as items (it will even update on the fly!)

Published at Wednesday, June 08, 2011 2:39 PM by Siva  

Hi mochy,

I am facing an problem in binding the ItemsSource using the Binding Path. The item source does not bind by using MVVM. When i bind directly in code behind it is working perfectly.

I tried by setting by the binding using the Dependency property but did not work as well.

You have mentioned that you were able to achive this control in MVVM. Can you please send me the complete solution to sivasankar.s4u@gmail.com

Thanks,

Siva

# re: WPF: ComboBox with CheckBoxes as items (it will even update on the fly!)

Published at Tuesday, July 05, 2011 3:36 PM by Sekhar  

where could i get this code sample.

# re: WPF: ComboBox with CheckBoxes as items (it will even update on the fly!)

Published at Thursday, July 07, 2011 1:45 PM by Praveen  

Plz Send source code of this article. Thanks

# re: WPF: ComboBox with CheckBoxes as items (it will even update on the fly!)

Published at Friday, July 08, 2011 8:36 AM by gurpreet  

can you please help me how to implement combobox with checkbox in windows form application(c sharp)......gurpreetsingh225@hotmail.com

# re: WPF: ComboBox with CheckBoxes as items (it will even update on the fly!)

Published at Monday, July 18, 2011 12:32 AM by Khyati  

If anyone knows how to implement this control in datagrid. Please share your code to khyati.shah@yahoo.co.in.

# re: WPF: ComboBox with CheckBoxes as items (it will even update on the fly!)

Published at Friday, July 22, 2011 11:20 AM by sivakumar  

hi   this  is  nice   artical    ,  but  i  dont  have   that  much   knowledge to  understand ,  please   explain  me  the  full atrical  particularlly  and  send this   to  my  mail address  sivakumarmr10@gmail.com  , thankq  very much

# re: WPF: ComboBox with CheckBoxes as items (it will even update on the fly!)

Published at Tuesday, August 09, 2011 1:28 PM by stukselbax  

Why do you use HierarchicalDataTemplate instead of just DataTemplate?

# re: WPF: ComboBox with CheckBoxes as items (it will even update on the fly!)

Published at Saturday, August 13, 2011 9:37 AM by siva kumar  

i had  used the Usercontrol but  iam  getting  This Error  So please  Rectify this error

error:

Provide value on 'System.Windows.Markup.StaticResourceHolder' threw an exception.

# re: WPF: ComboBox with CheckBoxes as items (it will even update on the fly!)

Published at Friday, August 19, 2011 12:21 AM by syntax  

Is there anyway I can uncheck all the checked items programatically?? I tried change in datasource (itemsource) but it didnt work out. What will be the best way to reinitilize these combos??

# re: WPF: ComboBox with CheckBoxes as items (it will even update on the fly!)

Published at Sunday, August 21, 2011 10:45 PM by Nir  

Great solution. Can you mail me the control code at nirnirkal@yahoo.com

# re: WPF: ComboBox with CheckBoxes as items (it will even update on the fly!)

Published at Tuesday, October 25, 2011 11:05 AM by Dayanand  

Great work buddy...but i'm find it hard to implement...i would appreciate if you could mail the solution on my email ID : dkale86@gmail.com

# re: WPF: ComboBox with CheckBoxes as items (it will even update on the fly!)

Published at Thursday, November 10, 2011 11:11 AM by Nishanth  

Can you please mail the solution at nishanth2@gmail.com? Thanks in advance.

# re: WPF: ComboBox with CheckBoxes as items (it will even update on the fly!)

Published at Friday, December 02, 2011 11:18 AM by Rohit Arora  

please mail solution to me at

rohit.arora@irissoftware.com

Thanks

# re: WPF: ComboBox with CheckBoxes as items (it will even update on the fly!)

Published at Sunday, January 01, 2012 7:56 AM by Ramakrishna Bariki  

I need a solutions hard to implement please mail

me : ramakrishna.bariki@gmail.com

# re: WPF: ComboBox with CheckBoxes as items (it will even update on the fly!)

Published at Friday, February 03, 2012 5:02 AM by Chandan  

this is excellent!! Great work!

# re: WPF: ComboBox with CheckBoxes as items (it will even update on the fly!)

Published at Wednesday, February 08, 2012 9:13 PM by pb  

can someone please mail the source to me at bhardwaj.cs@gmail.com

# re: WPF: ComboBox with CheckBoxes as items (it will even update on the fly!)

Published at Thursday, February 23, 2012 5:13 PM by itdev  

Great!

Can someone please mail the source code or better a small working project to me at hb.itdev@gmail.com

I'm not able to use the control without error at runtime

# re: WPF: ComboBox with CheckBoxes as items (it will even update on the fly!)

Published at Monday, February 27, 2012 1:42 PM by Venkat  

Hi

This is a great article and useful to me.But i need some more events.I have two groups of checkboxes country and state.When i select one checkbox in country combo then the relevant states of the country should appear in state combo.I am not able to fire any event which will refresh the state combo.Can someone help me to resolve the issue. It will be a great pleasure if you could send the solution to my mail urvenkatg@gmail.com

Thanks in advance

Venkat

# re: WPF: ComboBox with CheckBoxes as items (it will even update on the fly!)

Published at Saturday, March 03, 2012 11:34 PM by onuralp taner  

Please, post or send me an example?

E-Mail: onuralp at hotmail DOT com

Best Regards

# re: WPF: ComboBox with CheckBoxes as items (it will even update on the fly!)

Published at Tuesday, March 13, 2012 10:07 PM by Lazaro Castaño  

Genial code.....thanks a lot...

# re: WPF: ComboBox with CheckBoxes as items (it will even update on the fly!)

Published at Friday, March 16, 2012 8:01 AM by Shakti  

Some of the brushes used as StaticResource in the user control in the xaml file are missing. It would be great if you can add it.

# re: WPF: ComboBox with CheckBoxes as items (it will even update on the fly!)

Published at Tuesday, April 03, 2012 12:04 PM by sunny  

I am getting

Visual Studio designer gave an error dialog "Provide value on 'System.Windows.Markup.StaticResourceHolder' threw an exception."

when i try to use control in wpf form

# re: WPF: ComboBox with CheckBoxes as items (it will even update on the fly!)

Published at Thursday, April 12, 2012 4:44 PM by Alex  

Great work buddy...but i'm find it hard to implement...i would appreciate if you could mail the solution on my email ID : jjalexrayer@yahoo.com

# re: WPF: ComboBox with CheckBoxes as items (it will even update on the fly!)

Published at Friday, August 24, 2012 5:25 AM by cjw  

I try to get it work but seem to be not working well. Can some send me the code on my email: cjwiphone@hotmail.com. Thank you very much.

# re: WPF: ComboBox with CheckBoxes as items (it will even update on the fly!)

Published at Thursday, August 30, 2012 2:30 PM by prathi  

Hi its workling fine. but when i select a item and click directly on button results are not obtained as combo box requires a click event after the selection.

any suggestion about this.

thank u

# re: WPF: ComboBox with CheckBoxes as items (it will even update on the fly!)

Published at Wednesday, September 05, 2012 2:50 AM by Ken  

Remove the SolidBorderBrush in the xaml and it will remove the error

System.Windows.Markup.StaticResourceHolder' threw an exception  

# re: WPF: ComboBox with CheckBoxes as items (it will even update on the fly!)

Published at Thursday, November 22, 2012 3:03 PM by Valentino  

Good work! Can you mail me the control code and a working sample: at valefior AT inwind DOT it?

Thanks

# re: WPF: ComboBox with CheckBoxes as items (it will even update on the fly!)

Published at Saturday, December 22, 2012 4:30 PM by Abdul Samad  

I am using MS VS 2010. I have create component as you described on the top and created it in C#.

Now i want to use the component in VB.NET but it say 'Object reference not set to an instance of object'

Please advise

Would you please upload ZIP file with latest c# code, which i can use in VB.NET?

# UserControl Checkbox Dynamic Content Value

Published at Sunday, January 20, 2013 2:37 AM by UserControl Checkbox Dynamic Content Value  

Pingback from  UserControl Checkbox Dynamic Content Value

# re: WPF: ComboBox with CheckBoxes as items (it will even update on the fly!)

Published at Tuesday, March 12, 2013 10:14 AM by Gowtham  

Great solution it is Working fine, but how to implement Cascading Drop Down lists,, in my project i have to develop it for States, Districts and Mandals

Leave a Comment

(required) 
(
required
)
 
(optional)
(required) 

Enter the numbers above: