孔唯妍

奋斗ing

WPF中DataGrid 添加checkbox实现全选、多选

   在机房合作项目中,有一个窗体是这样子的

         

    简单介绍一下:就是通过查询出正在上机的学生,可以通过勾选前面的checkbox,选中该行,然后可以进行部分下机和全部下机。如果想要全选,就勾选上图画红框的checkbox。


实现代码:

xaml:

<span style="font-family:KaiTi_GB2312;font-size:24px;"><Grid>
        <DataGrid x:Name="DgLineState" HorizontalAlignment="Left" Margin="30,27,0,0" VerticalAlignment="Top" Height="167" Width="645" FontFamily="Microsoft YaHei Light" FontSize="24"  ItemsSource="{Binding}" AutoGenerateColumns="False" EnableColumnVirtualization="False">
        <DataGrid.Columns>
                <DataGridTemplateColumn>
                    <DataGridTemplateColumn.HeaderTemplate>
                        <DataTemplate>
                            <CheckBox x:Name="CheckAll" Click="CheckAll_Click" Width="42" />
                        </DataTemplate>
                    </DataGridTemplateColumn.HeaderTemplate>
                </DataGridTemplateColumn>
                <DataGridTextColumn Width="15*" Header="卡号" Binding="{Binding cardid}" />
                <DataGridTextColumn Width="15*" Header="教师号" Binding="{Binding userid }" />
                <DataGridTextColumn Width="15*" Header="上机日期" Binding="{Binding onlinedate}"/>
                <DataGridTextColumn Width="15*" Header="上机时间" Binding="{Binding onlinetime}"/>
                <DataGridTextColumn Width="15*" Header="机号" Binding="{Binding computer}"/>
        </DataGrid.Columns >
        </DataGrid></span>

Cs代码:

<span style="font-family:KaiTi_GB2312;font-size:24px;">public class AAA : INotifyPropertyChanged
        {
            private bool isSelected = false;

            public bool IsSelected
            {
                get { return isSelected; }
                set { isSelected = value; GetChanged("IsSelected"); }
            }

            private string name;

            public string Name
            {
                get { return name; }
                set { name = value; GetChanged("Name"); }
            }

            public event PropertyChangedEventHandler PropertyChanged;

            private void GetChanged(string Name)
            {
                if (PropertyChanged != null)
                {
                    PropertyChanged(this, new PropertyChangedEventArgs(Name));
                }
            }

           
        }

        private void CheckAll_Click(object sender, RoutedEventArgs e)
        {
            foreach (var item in DgLineState.Items)
            {
                DataGridTemplateColumn templeColumn = DgLineState.Columns[0] as DataGridTemplateColumn;
                FrameworkElement fwElement = DgLineState.Columns[0].GetCellContent(item);

                if (fwElement != null)
                {
                    CheckBox cBox = templeColumn.CellTemplate.FindName("cb", fwElement) as CheckBox;
                    if (cBox.IsChecked == true)
                    {
                        cBox.IsChecked = false;
                    }
                    else
                    {
                        if (cBox != null)
                        {
                            cBox.IsChecked = true;
                        }
                        else
                        {
                            cBox.IsChecked = false;
                        }
                    }

                }

            }
        }</span>

添加一个viewmodel类

<span style="font-family:KaiTi_GB2312;font-size:24px;">using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.ComponentModel;
namespace JfCooperate.一般用户
{
   public  class viewmodel:INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        private void INotifyPropertyChanged(string name)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(name));
            }
        }

        private int xh;

        public int Xh
        {
            get { return xh; }
            set { xh = value; }
        }

        private string name;

        public string Name
        {
            get { return name; }
            set
            {
                name = value;
                INotifyPropertyChanged("Name");
            }
        }

        private int age;

        public int Age
        {
            get { return age; }
            set
            {
                age = value;
                INotifyPropertyChanged("Age");
            }
        }

        private bool isSelected;

        public bool IsSelected
        {
            get { return isSelected; }
            set
            {
                isSelected = value;
                INotifyPropertyChanged("IsSelected");
            }
        }
    }
}
</span>




阅读更多
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/kwy15732621629/article/details/52035466
所属专栏: wpf简单应用
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

不良信息举报

WPF中DataGrid 添加checkbox实现全选、多选

最多只允许输入30个字

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭