WPF Checked ListBox

WPF currently does not have a checked list box out of the box so you’ll need to roll your own. Unfortunately most of the examples that come up on Google involve creating a usercontrol and writing some code.

Here’s one quick way that does not not involve writing any additional code.

Step 1: Start off by creating a class that will represent each checked list item in the list box. Obviously if you already have your data item all you need to ensure is that it has a boolean property to store the Checked/Unchecked flag.

public class CheckedListItem
{
    public int Id { get; set; }
    public string Name { get; set; }
    public bool IsChecked { get; set; }
}

 

Step 2: Create the list item that will be bound to the list box. I called my list AvailablePresentationObjects.

public List<CheckedListItem> AvailablePresentationObjects;

Step 3: The last step is to create the actual checked list box. I created a list box and used the HierarchicalDataTemplate to hold the CheckBox. The Name and the IsChecked property are then bound to the checkbox.

<ListBox ItemsSource="{Binding AvailablePresentationObjects}" >
    <ListBox.ItemTemplate>
        <HierarchicalDataTemplate>
            <CheckBox Content="{Binding Name}" IsChecked="{Binding IsChecked}"/>
        </HierarchicalDataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

And walla!

WPF-CheckedListBox

Now the IsChecked property will reflect the value in the UI and vice versa. If you need real-time notification when someone checks/unchecks then you simply need to raise the PropertyChanged event in the CheckedListItem class.

  • http://sharepointsix.blogspot.com Doug

    Very concise implementation you got there. :)

  • Tech-Mentor

    I love your solution, simple and elegant.

    I do have a little problem with it. In your example you do not instanciate the List object which is required if you need to prefill the list.

    Also the ItemsSource binding in XAML does not work. I have to manually set the property in code.

  • Console.WriteLine

    @ Tech-Mentor Says

    You have to create an object data source with x:Key property set to ‘AvailablePresentationObjects’ or a DependencyProperty in the parent container … Binding in XaML with object sources are a little bit different but IT does work.

    Personally I do prefer setting it in code behind when building a data driven solution – different scenarios need different solutions.

  • Vdangwal

    do you have any solution for multiselect combobox in wpf?

  • suckmycode

    Excellent, just what I was looking for, thanks!

  • http://twitter.com/JeffFerguson Jeff Ferguson

    Nice article!

    (But, it’s “voila”, not “walla”).

  • Raing65

    I am a newbie in WPF and I tried to follow your code. How do you do Step 2. I seems incomplete based on a newbees perspective.

  • Rick Davis

    Appreciate this article a lot – but I wish you would have completed it with a good example of loading it up. You mentioned setting the key property, but I am too confused to figure it out. Not sure what to do with AvailablePresentationObjects

  • Annapurna

    Great Article.

    I have a question,how do we implement itemtemplate for listbox in code behind class.

  • sadique

    thanks a lot …………

  • SkyZoThreaD

    actually it’s “voilà”. But you probably wrote this on a querty kb ;-)