ARTICLE

CheckBoxList in WPF

Posted by Syed Shakeer Articles | WPF February 26, 2011
This Article explains how to create a CheckBoxList and how to get the Text and Value of a checked CheckBoxListItem in WPF.
Reader Level:


This Article explains how to create a CheckBoxList and how to get the Text and Value of a checked CheckBoxListItem in WPF.

First create a CheckBoxList using ListBox with DataTemplate as follows in XAML.

<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="CheckBoxLsit" Height="350" Width="525">
<
Grid>
<
StackPanel Width="250" Height="80">
<
ListBox Name="listBoxZone" ItemsSource="{Binding TheList}"
 Height="115" Background="Azure">
<
ListBox.ItemTemplate>
<
DataTemplate>
 <
CheckBox Name="CheckBoxZone" Content="{Binding TheText}
     Tag="{Binding TheValue}" Checked="CheckBoxZone_Checked"  
     Margin
="0,5,0,0"/>
</
DataTemplate
</
ListBox.ItemTemplate>
</
ListBox>
</
StackPanel>
<
TextBox Name="ZoneText" Width="160" Height="20" Margin="-80,140,0,0" Background="Bisque" />
<
TextBox Name="ZoneValue" Width="160" Height="20" Margin="-80,190,0,0" Background="Bisque" />
</
Grid>
</
Window>

Binding data to CheckBoxList in .XAML.Cs

Using System.Collections.ObjectModel;
namespace WpfApplication1
{
/// <summary>
///
Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public ObservableCollection<BoolStringClass> TheList {get; set ;}
public MainWindow()
{
InitializeComponent();
CreateCheckBoxList();
}
public class BoolStringClass
{
       public string TheText { get; set; }
       public int TheValue { get; set; }
}
public void CreateCheckBoxList()
{
TheList =
new ObservableCollection<BoolStringClass>();
TheList.Add(
new BoolStringClass { TheText = "EAST" ,TheValue =1});
TheList.Add(
new BoolStringClass { TheText ="WEST",TheValue =2});
TheList.Add(
new BoolStringClass { TheText ="NORTH", TheValue=3});
TheList.Add(
new BoolStringClass {TheText ="SOUTH", TheValue =4});
this.DataContext =this;
}


Get the Text and value of Checked CheckBoxList

Write the code below in the checkBox Checked Event.

private void CheckBoxZone_Checked(object sender, RoutedEventArgs e)
{
CheckBox chkZone = (CheckBox)sender;
ZoneText.Text =
"Selected Zone Name= "+ chkZone.Content.ToString();
ZoneValue.Text =
"Selected Zone Value= " + chkZone.Tag.ToString();
}

wpf.gif

Thanks for Reading my article!

Login to add your contents and source code to this article
post comment
     

Orignal source : http://social.msdn.microsoft.com/Forums/eu/wpf/thread/a355a085-2650-45b6-a75b-b6a298e3f20d

Posted by Abhishek joshi Jul 31, 2012

Nice demo appreciate it. What about if you are using the M-V-VM pattern. I understand that you can't have code-behind and everything is declarative. I have a similar problem in my project where I have to use a winforms checkboxlist using interop but I don't know how to data bind it to my ViewModel. Thanks, Roland

Posted by rolando cruz Mar 23, 2011
COMMENT USING
PREMIUM SPONSORS
DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and add new content to existing PDF documents from within your applications.
Join a Chapter
SPONSORED BY
  • PDF reports have never been easier to create. With our included WYSIWYG Designer, you can layout your reports, set up your data source and let DynamicPDF ReportWriter do the rest.