Click here to Skip to main content
15,884,836 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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
C#
<ListBox x:Name="listExtraSkills" ItemsSource="{Binding CheckList}" Grid.Column="1" Grid.Row="6" Height="50" >
           <ListBox.ItemTemplate>
               <DataTemplate>
                   <CheckBox Name="chkitems" Content="{Binding TheText}" IsChecked="{Binding IsSelected }"  > </CheckBox>
               </DataTemplate>
           </ListBox.ItemTemplate>
       </ListBox>
Posted

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

First of all, the code-behind:
C#
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

        //Constructor
        public Window1()
        {
            InitializeComponent();
            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:
XML
<window x:class="WpfApplicationCheckBoxList.Window1" xmlns:x="#unknown">
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1" 
    Height="400" 
    Width="600"
    DataContext="{Binding RelativeSource={RelativeSource Self}}">
    <grid>
        <listbox x:name="listExtraSkills" itemssource="{Binding CheckList}" grid.column="1" grid.row="6" height="50">
            <listbox.itemtemplate>
                <datatemplate>
                    <checkbox name="chkitems" content="{Binding TheText}" ischecked="{Binding IsSelected }"></checkbox>
                </datatemplate>
            </listbox.itemtemplate>
        </listbox>
    </grid>
</window>


And finally the data object you're binding to the list:
C#
public class CheckListItem
{
    public string TheText { get; set; }
    public bool IsSelected { get; set; }
}
 
Share this answer
 
v2
Comments
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
Adam David Hill 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)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900