Click here to Skip to main content
13,003,589 members (62,306 online)
Rate this:
Please Sign up or sign in to vote.
i want checkboxlist in Wpf
using xaml i got the checkboxlist in design but i don't know how to use this in code this is my xaml code
<ListBox x:Name="listExtraSkills" ItemsSource="{Binding CheckList}" Grid.Column="1" Grid.Row="6" Height="50" >
                   <CheckBox Name="chkitems" Content="{Binding TheText}" IsChecked="{Binding IsSelected }"  > </CheckBox>
Posted 20-Jun-12 0:08am
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

Here's a working example, based on your XAML:

First of all, the code-behind:
using System.Collections.Generic;
using System.ComponentModel;
using System.Windows;
using System.Windows.Documents;
namespace WpfApplicationCheckBoxList
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class Window1 : Window, INotifyPropertyChanged
        #region Instance Variables
        List<checklistitem> checkList;
        #endregion /Instance Variables
        #region INotifyPropertyChanged
        //Add this bit of code to all your code behinds
        public event PropertyChangedEventHandler PropertyChanged;
        protected virtual void OnPropertyChanged(PropertyChangedEventArgs e)
            if (PropertyChanged != null)
                PropertyChanged(this, e);
        #endregion /INotifyPropertyChanged
        public Window1()
            CheckList = GenerateTestData();
        //Property to bind to
        public List<checklistitem> CheckList
            get { return checkList; }
            set { checkList = value; this.OnPropertyChanged(new PropertyChangedEventArgs("CheckList")); }
        //Test data helper
        List<checklistitem> GenerateTestData()
            List<checklistitem> checkListItems = new List<checklistitem>();
            checkListItems.Add(new CheckListItem { TheText = "My Text Here 1", IsSelected = false });
            checkListItems.Add(new CheckListItem { TheText = "My Text Here 2", IsSelected = true });
            checkListItems.Add(new CheckListItem { TheText = "My Text Here 3", IsSelected = false });
            return checkListItems;

Remember to set the DataContext in your XAML:
<window x:class="WpfApplicationCheckBoxList.Window1" xmlns:x="#unknown">
    DataContext="{Binding RelativeSource={RelativeSource Self}}">
        <listbox x:name="listExtraSkills" itemssource="{Binding CheckList}" grid.column="1" grid.row="6" height="50">
                    <checkbox name="chkitems" content="{Binding TheText}" ischecked="{Binding IsSelected }"></checkbox>

And finally the data object you're binding to the list:
public class CheckListItem
    public string TheText { get; set; }
    public bool IsSelected { get; set; }
Sravanthid28 20-Jun-12 7:06am
if we want dynamically check the check box and respected checkedbox content store into the database?
can you please tell code for this
AdamDavidHill 20-Jun-12 7:14am
To manipulate the values you'd write something like: CheckList[2].IsSelected = true; - i.e. you're manipulating the values of the CheckList property. To add a new check box you'd use CheckList.Add(new CheckListItem{... etc. Whether or not you store this in a database or not is a separate question and should probably be asked in another thread. Hope that helps. Please mark as the answer if this solves the question of how to use that XAML with code behind.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web01 | 2.8.170626.1 | Last Updated 20 Jun 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100