Friday, May 31, 2013

How to Create CheckBox Datagrid in Silverlight

Sometimes we need to choose one or more record. So for this reason we need to create checkbox grid. Like as follows.
I have developed it in silverlight 5 with mvvm architectural pattern.

in xaml  

 
<UserControl



xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

xmlns:d="http://schemas.microsoft.com/expression/blend/2008"

xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"

xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk"

x:Class="CheckBoxGrid.MainPage"

xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"

mc:Ignorable="d"

d:DesignHeight="300" d:DesignWidth="1300">

<Grid x:Name="LayoutRoot" Background="White">

<Grid.RowDefinitions>

<RowDefinition></RowDefinition>

</Grid.RowDefinitions>



<sdk:DataGrid

HorizontalAlignment="Stretch" VerticalAlignment="Top" Grid.Row="0" Grid.Column="0"

ItemsSource="{Binding Path=VMVStudent.StudentList,Mode=TwoWay}"

SelectionMode="Single"

IsReadOnly="True"

AutoGenerateColumns="False"

>

<sdk:DataGrid.Columns>

<sdk:DataGridTemplateColumn Width="20">

<sdk:DataGridTemplateColumn.HeaderStyle >

<Style TargetType="sdk:DataGridColumnHeader">

<Setter Property="Template">

<Setter.Value>

<ControlTemplate>

<Grid>

<Grid.ColumnDefinitions>

<ColumnDefinition></ColumnDefinition>

</Grid.ColumnDefinitions>

<CheckBox x:Name="chkAll" IsChecked="{Binding Path=VMVStudent.IsSelectAll, Mode=TwoWay}" Grid.Column="0">

<i:Interaction.Triggers>

<i:EventTrigger EventName="Click">

<i:InvokeCommandAction Command="{Binding SelectAllCommand, Source={StaticResource StudentSR}}" CommandParameter="{Binding ElementName=chkAll}" />

</i:EventTrigger>

</i:Interaction.Triggers>

</CheckBox>

</Grid>

</ControlTemplate>

</Setter.Value>

</Setter>

</Style>

</sdk:DataGridTemplateColumn.HeaderStyle>

<sdk:DataGridTemplateColumn.CellTemplate >

<DataTemplate>

<CheckBox Name="chkSelect" IsChecked="{Binding Path=IsSelect,Mode=TwoWay}"/>

</DataTemplate>

</sdk:DataGridTemplateColumn.CellTemplate>



</sdk:DataGridTemplateColumn>

<sdk:DataGridTextColumn Binding="{Binding Path=Id}" Width="*" Header="Student Id"></sdk:DataGridTextColumn>

<sdk:DataGridTextColumn Binding="{Binding Path=Name}" Width="*" Header="Student Name"></sdk:DataGridTextColumn>

</sdk:DataGrid.Columns>

</sdk:DataGrid>





</Grid>

</UserControl>

Page Code Behind
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
namespace CheckBoxGrid
{
    public partial class MainPage : UserControl
    {
        VM.VMSudent _studentVm = null;
        public MainPage()
        {
            _studentVm = new VM.VMSudent();
            Resources.Add("StudentSR", _studentVm);
            InitializeComponent();
            this.DataContext = _studentVm;
        }
    }
}


In Model
 using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
 namespace CheckBoxGrid.Model
{
    public class Students:BaseClass
    {
        int id;
        string name;        public string Name
        {
            get { return name; }
            set { name = value;
            NotifyPropertyChanged("Name");
           
            }
        }        public int Id
        {
            get { return id; }
            set { id = value;
            NotifyPropertyChanged("Id");
            }
        }
        private bool isSelect;        public bool IsSelect
        {
            get { return isSelect; }
            set { isSelect = value;
            NotifyPropertyChanged("IsSelect");
            }
        }    }
}
In VMV
  using System;
using System.Collections.ObjectModel;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using CheckBoxGrid.Model;

namespace CheckBoxGrid.VMV
{
    public class VMVStudent : BaseClass
    {
        private ObservableCollection studentList = new ObservableCollection();
        public ObservableCollection StudentList
        {
            get { return studentList; }
            set
            {
                studentList = value;
                NotifyPropertyChanged("StudentList");
            }
        }
        private bool _IsSelectAll;

        public bool IsSelectAll
        {
            get { return _IsSelectAll; }
            set { _IsSelectAll = value;
            NotifyPropertyChanged("IsSelectAll");
            }
        }
    }
}
In VM

using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using NEXTIT.ERP.LIB;
namespace CheckBoxGrid.VM
{
    public class VMSudent:BaseClass
    {
        private VMV.VMVStudent _VMVStudent = new VMV.VMVStudent();
        public VMV.VMVStudent VMVStudent
        {
            get { return _VMVStudent; }
            set { _VMVStudent = value; }
        }
        public VMSudent() {
            PopulateStudentList();
            SelectAllCommand = new DelegateCommand(OnSelectAllCommand, CanLoadCustomers);
        }
        private void PopulateStudentList()
        {
            VMVStudent.StudentList = new System.Collections.ObjectModel.ObservableCollection {
            new Model.Students {Id=1,Name = "Pria"},
            new Model.Students {Id=2,Name = "Smith"},
            new Model.Students {Id=3,Name = "Devid"},
            new Model.Students {Id=4,Name = "Helen"},
            new Model.Students {Id=5,Name = "Nuton"},
            new Model.Students {Id=6,Name = "Kafre"},
            };
        }
        ///
        /// Loads the customers.
        ///

        /// The parameter.
        private void OnSelectAllCommand(object parameter)
        {

            if (((dynamic)(parameter)).IsChecked == true)
            {
                _VMVStudent.IsSelectAll = true;
                foreach (var obj in VMVStudent.StudentList)
                {
                    obj.IsSelect = true;
                }
            }
            else {
                _VMVStudent.IsSelectAll = false;
                foreach (var obj in VMVStudent.StudentList)
                {
                    obj.IsSelect = false;
                }
           
            }
        }
        public ICommand SelectAllCommand { get; set; }
        ///
        /// Determines whether this instance [can load customers] the specified parameter.
        ///

        /// The parameter.
        ///
        ///  true if this instance [can load customers] the specified parameter;
        ///  otherwise, false.
        ///

        private bool CanLoadCustomers(object parameter)
        {
            return true;
        }
       
    }
}

 

 


 


 
 



No comments:

Post a Comment