WPF/SLIVERLIGHT DATAGRID全选全不选

<!--XAML-->
    <Window x:Class="WPFTest.Window3"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Window3" Height="300" Width="300">
    <Grid>
        <DataGrid x:Name="datagrid"   HorizontalContentAlignment="Left"  Margin="5,5,5,62" AutoGenerateColumns="False"  BorderBrush="White"  Background="White"    >
            <DataGrid.Columns >
                <DataGridTemplateColumn >
                    <DataGridTemplateColumn.HeaderTemplate>
                        <DataTemplate>
                            <CheckBox  HorizontalAlignment="Center" Name="ckall" VerticalAlignment="Center" Click="ckall_Click" Tag="{Binding}" />
                        </DataTemplate>
                    </DataGridTemplateColumn.HeaderTemplate>
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <CheckBox Name="cb"  IsChecked="{Binding IsChecked,Mode=TwoWay}" Click="cb_Click" Tag="{Binding}"></CheckBox>
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>
                <DataGridTextColumn Header="名字"  Binding="{Binding Name}" ></DataGridTextColumn>
                <DataGridTextColumn Header="年龄" Binding="{Binding Age}"></DataGridTextColumn>
                <DataGridTextColumn Header="地址" Binding="{Binding Addrress}"></DataGridTextColumn>
            </DataGrid.Columns>
        </DataGrid>
        <Button  Height="23" HorizontalAlignment="Left" Content="确定" Margin="28,221,0,0" Name="button1" VerticalAlignment="Top" Width="75" Click="button1_Click" />
    </Grid>
</Window>

//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.Shapes;
using System.Data;
using System.Collections;
using System.IO;
using System.ComponentModel;

namespace WPFTest
{
    /// <summary>
    /// Window3.xaml 的交互逻辑
    /// </summary>
    public partial class Window3 : Window
    {
        public Window3()
        {
            InitializeComponent();
            List<Person> list = new List<Person>() 
        {
            new Person(){Name="Tom",Adress="China",Age=23},
            new Person(){Name="LiLie",Adress="China",Age=11},
            new Person(){Name="WangXiaoMing",Adress="China",Age=23},
        };
            datagrid.ItemsSource = list;
        }

        private void cb_Click(object sender, RoutedEventArgs e)
        {
            CheckBox chk = (CheckBox)sender;
            Person per = chk.Tag as Person;
            per.IsChecked = chk.IsChecked.Value;
        }

        private void ckall_Click(object sender, RoutedEventArgs e)
        {
            CheckBox allck = (CheckBox)sender;
            List<Person> perList = this.datagrid.ItemsSource as List<Person>;
            foreach (var item in perList)
            {
                item.IsChecked = allck.IsChecked.Value;
            }
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            List<Person> choosePerson = new List<Person>();
            if ((this.datagrid.ItemsSource as List<Person>).Count > 0)
            {
                foreach (var item in this.datagrid.ItemsSource as List<Person>)
                {
                    if (item.IsChecked == true)
                    {
                        choosePerson.Add(item);
                    }
                }
            }
            if (choosePerson.Count > 0)
            {
                //选择的数据
            }
        }
    }
    public class Person : INotifyPropertyChanged
    {
        public void INotifyPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
        public event PropertyChangedEventHandler PropertyChanged;
        public string Name { get; set; }
        public string Adress { get; set; }
        public int Age { get; set; }

        private bool isChecked;

        public bool IsChecked
        {
            get { return isChecked; }
            set
            {
                isChecked = value;
                INotifyPropertyChanged("IsChecked");
            }
        }
    }
}

 

你可能感兴趣的:(DATAGRID全选全不选,WPF/SLIVERLIGHT)