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}" >
            <CheckBox Content="{Binding Name}" IsChecked="{Binding IsChecked}"/>

And walla!


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.

WPF Checked ListBox

12 thoughts on “WPF Checked ListBox

  1. Tech-Mentor says:

    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.

  2. Console.WriteLine says:

    @ 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.

  3. Raing65 says:

    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.

  4. Rick Davis says:

    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

  5. Annapurna says:

    Great Article.

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

Comments are closed.