Email: Password:       Forgot Password 
    .netCodeSG
A Saarsha Group Online Community for dot net codes group like C#, Asp.NET, VB.NET, Sharepoint, JavaScript, JQuery, Ajax, SQL, WCF, WPF.
 
TECHNOLOGIES:
 

CheckBoxList in WPF

Article:
Viewed:  5690 
Posted On:  20/09/2012 10:01:01 
In this artilce I am going to show how we can use CheckBoxList in WPF 

In this article I am going to show how we can use CheckBoxList in WPF.  

This is my XAML code..

<Window x:Class="CheckBoxList_WPF.Window1"   xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"    Title="Window1" Height="200" Width="300">
    <Grid>
        <ListBox Name="lstItem" ItemsSource="{Binding myItemList}" Height="115" Background="LightGreen" Margin="10,10,10,10">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <CheckBox Name="chkItem" Content="{Binding itemText}"  Tag="{Binding itemValue}" Checked="chkItem_Checked"  Margin="0,5,0,0"/>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
    </Grid>
</Window> 

This is my XAML.cs code.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Collections.ObjectModel; 

namespace CheckBoxList_WPF
{

    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>

    public partial class Window1 : Window
    {
        public ObservableCollection<BoolStringClass> myItemList { get; set; } 

        public Window1()
        {
            InitializeComponent();
            GetMyItemList();
        } 

        public class BoolStringClass
        {
            public string itemText { get; set; }
            public int itemValue { get; set; }
        } 

        public void GetMyItemList()
        {
            myItemList = new ObservableCollection<BoolStringClass>();
            myItemList.Add(new BoolStringClass { itemText = "CRICKET", itemValue = 1 });
            myItemList.Add(new BoolStringClass { itemText = "HOCKEY", itemValue = 2 });
            myItemList.Add(new BoolStringClass { itemText = "TENNIS", itemValue = 3 });
            myItemList.Add(new BoolStringClass { itemText = "FOOTBALL", itemValue = 4 });
            this.DataContext = this;
        } 

        private void chkItem_Checked(object sender, RoutedEventArgs e)
        {
            CheckBox chkSelecttedItem = (CheckBox)sender;
            MessageBox.Show("Selected Item Name= " + chkSelecttedItem.Content.ToString() + System.Environment.NewLine + "Selected Item Value= " + chkSelecttedItem.Tag.ToString());
        }
    }
} 

When I run my application

ChK1.png

Figure 1.

When I select any check box then

Chk2.png

Figure 2.

  Comment:
         HOME   |   Submit Article   |   Contact Us   |   About Us   |   Terms & Condition   |   Advertise With us