.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Articles >> WPF >> Post New Resource Bookmark and Share   

 Subscribe to Articles

CheckBoxList in WPF

Posted By:Syed Shakeer Hussain       Posted Date: January 30, 2011    Points: 75    Category: WPF    URL: http://www.dotnetspark.com  

This Article Explains you how to create a CheckBoxList and How to get the Text and Value of Checked CheckBoxListItem in WPF.
 

First Creating a CheckBoxLsit by 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>

Bindind data to CheckBoxList in .XAML.Cs

using System.Collections.ObjectModel;

namespace WpfApplication1

{

///

/// Interaction logic for MainWindow.xaml

///

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 below code in 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();

}

 

Thanks for Reading my article!

Shakeer


 Subscribe to Articles

     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More Articles on C#, ASP.Net, Vb.Net, SQL Server and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend