Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#3.0 WPF wpftoolkit
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" >
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <CheckBox Name="chkitems" Content="{Binding TheText}" IsChecked="{Binding IsSelected }"  > </CheckBox>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
Posted 20-Jun-12 1:08am
Rate this: bad
good
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
 
        //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:
<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}}">
 
And finally the data object you're binding to the list:
    public class CheckListItem
    {
        public string TheText { get; set; }
        public bool IsSelected { get; set; }
    }
  Permalink  
v2
Comments
Sravanthid28 at 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 at 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
0 OriginalGriff 190
1 Jochen Arndt 155
2 PIEBALDconsult 150
3 Afzaal Ahmad Zeeshan 120
4 DamithSL 115
0 OriginalGriff 5,695
1 DamithSL 4,591
2 Maciej Los 4,012
3 Kornfeld Eliyahu Peter 3,480
4 Sergey Alexandrovich Kryukov 3,190


Advertise | Privacy | Mobile
Web03 | 2.8.141220.1 | Last Updated 20 Jun 2012
Copyright © CodeProject, 1999-2014
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