zoukankan      html  css  js  c++  java
  • Wpf DataGrid 数据绑定 排序 删除

    初学wpf今天做一个菜鸟级别实例,只适合菜鸟.

      先上图

      

      说明一下功能:显示总条数,选中条数.全选.点击datagrid勾选,也可多选

      datagrid绑定的数据是用的一个数据过渡类,如有一个Student类,类中有Id,Name...

      但没有总条数也没有当前选中条数(当然还有其他情况会跟这种情况类似时也会用到)

      所以我就构造一个数据过渡类StudentData

      //数据实体类代码

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    public class PropertyChangedBase : INotifyPropertyChanged
        {
            public event PropertyChangedEventHandler PropertyChanged;
            public void Notify(string propertyName)
            {
                if (PropertyChanged != null)
                {
                    PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
                }
            }
        }
        public class StudentData : PropertyChangedBase
        {
            private Student _student;
            public Student student
            {
                get { return _student; }
                set { _student = value; Notify("student"); }
            }
            private int _Count=0;
            /// <summary>
            /// 总条数
            /// </summary>
            public int Count
            {
                get { return _Count; }
                set { _Count = value; Notify("Count"); }
            }
            private int _IsCheckedCount=0;
            /// <summary>
            ///  当前选中条数
            /// </summary>
            public int IsCheckedCount
            {
                get { return _IsCheckedCount; }
                set { _IsCheckedCount = value; Notify("IsCheckedCount"); }
            }
        }
    
        public class Student : PropertyChangedBase
        {
            private string _Id;
            public string Id
            {
                get { return _Id; }
                set { _Id = value; Notify("Id"); }
            }
            private string _Name;
            public string Name
            {
                get { return _Name; }
                set { _Name = value; Notify("Name"); }
            }
            public bool _IsChecked;
            /// <summary>
            /// 是否选中
            /// </summary>
            public bool IsChecked
            {
                get { return _IsChecked; }
                set { _IsChecked = value; Notify("IsChecked"); }
            }               
        }
    

      数据实体已经写好,则需要绑定数据了,绑定数据代码

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    /// <summary>
            /// 数据过渡类集合
            /// </summary>
            List<StudentData> LStudentData = new List<StudentData>();
            List<Student> stuList { get; set; }
            public void GetData()
            {
                //获取学生数据集合
                stuList = new List<Student> { 
                    new Student{Id="001",Name="zenghai1",IsChecked=false},
                    new Student{Id="002",Name="zenghai2",IsChecked=false},
                    new Student{Id="003",Name="zenghai3",IsChecked=false},
                    new Student{Id="004",Name="zenghai4",IsChecked=false},
                    new Student{Id="005",Name="zenghai5",IsChecked=false},
                };
                //循环学生数据,并添加到数据过渡集合中
                foreach (var item in stuList)
                {
                    StudentData listStu = new StudentData();
                    listStu.student = item;
                    listStu.Count = stuList.Count;
                    listStu.IsCheckedCount = stuList.Count(p => p.IsChecked == true);
                    LStudentData.Add(listStu);
                }
                //绑定
                Grid_Data.DataContext = LStudentData;
    
            }
    

      既然数据已经绑定好了,那就贴出xmal的代码,注意datagrid的 CanUserAddRows属性得置为false,不然DataGrid会

      在尾部多出一行,意思为是否允许用户增加行.

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    <Grid Name="Grid_Data">
            <Grid.RowDefinitions>
                <RowDefinition Height="30" />
                <RowDefinition Height="*" /> 
            </Grid.RowDefinitions>
            <Grid Grid.Row="0">
                <StackPanel Orientation="Horizontal">
                <TextBlock VerticalAlignment="Center">
                    <TextBlock Text="当前总条数"></TextBlock>                
                    <TextBlock Text="{Binding Path=Count}"></TextBlock>
                    <TextBlock Text="条"></TextBlock>
                </TextBlock>                
                    <TextBlock  VerticalAlignment="Center" Margin="10,0,0,0" >
                        <TextBlock Text="当前选中条数"></TextBlock>                
                    <TextBlock x:Name="Tb_SelectCount" Text="{Binding Path=IsCheckedCount}"></TextBlock>
                    <TextBlock Text="条"></TextBlock>
                    </TextBlock>
                    <Button Name="btn_delete" Click="btn_delete_Click" Content="删除选中"  VerticalAlignment="Center" Margin="10,0,0,0"></Button>
                </StackPanel>
            </Grid>
            <Grid Grid.Row="1">
                <DataGrid Name="DgQuestion" ItemsSource="{Binding }"  Margin="0" Background="White" SelectionChanged="DgQuestion_SelectionChanged" CanUserAddRows="False" AutoGenerateColumns="False"  HorizontalGridLinesBrush="#FFD1CFCF" VerticalGridLinesBrush="#FFD1CFCF">
                <DataGrid.CellStyle>
                    <Style TargetType="DataGridCell">
                        <Style.Triggers>
                            <Trigger Property="IsSelected" Value="True">
                                <Setter Property="Background" Value="#DBEDF8"/>
                                <Setter Property="BorderBrush" Value="#DBEDF8"/>
                                <Setter Property="Foreground" Value="Black"/>
                            </Trigger>
                        </Style.Triggers>
                    </Style>
                </DataGrid.CellStyle>
                <DataGrid.Columns>
                        <DataGridTemplateColumn>
                        <DataGridTemplateColumn.Header>
                            <CheckBox Content="全 选" x:Name="cBox_All" Click="cBox_All_Click"></CheckBox>
                        </DataGridTemplateColumn.Header>
                           
                            <DataGridTemplateColumn.CellTemplate>
                            <DataTemplate>
                                    <CheckBox IsChecked="{Binding Path=student.IsChecked}"></CheckBox>                                
                            </DataTemplate>
                        </DataGridTemplateColumn.CellTemplate>
                    </DataGridTemplateColumn>
                        <DataGridTextColumn Header="名称" Width="*" Binding="{Binding Path=student.Name}" IsReadOnly="True"/>                
                </DataGrid.Columns>
            </DataGrid>
            </Grid>       
        </Grid>
    

      以下是页面中的事件处理

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    //全选按钮事件
            private void cBox_All_Click(object sender, RoutedEventArgs e)
            {
                CheckBox cb = sender as CheckBox;
                LStudentData.FindAll(p =>
                {
                    p.student.IsChecked = cb.IsChecked.Value;
                    p.Count = stuList.Count;                 
                    return true; });
                LStudentData.FindAll(p => { p.IsCheckedCount = stuList.Count(t => t.IsChecked == true); return true; });
            }
    
    
            //DataGrid SelectionChanged
            private void DgQuestion_SelectionChanged(object sender, SelectionChangedEventArgs e)
            {
                if (DgQuestion.SelectedItems.Count > 1)
                {
                    foreach (var item in e.AddedItems)
                    {
                        if (item is StudentData)
                        {
                            bool isChecked = (item as StudentData).student.IsChecked;
                            (item as StudentData).student.IsChecked = true;
                        }
                    }
                }
                else
                {
                    if (e.AddedItems.Count ==1)
                    {
                        if (e.AddedItems[0] is StudentData)
                        {
                            bool isChecked = (e.AddedItems[0] as StudentData).student.IsChecked;
                            if (isChecked)
                                (e.AddedItems[0] as StudentData).student.IsChecked = false;
                            else
                                (e.AddedItems[0] as StudentData).student.IsChecked = true;
                        }
                    } 
                }
                LStudentData.FindAll(p => { p.IsCheckedCount = stuList.Count(t => t.IsChecked == true); return true; });
            }        
            //删除选中按钮事件
            private void btn_delete_Click(object sender, RoutedEventArgs e)
            {
                LStudentData = LStudentData.FindAll(p => { if (p.student.IsChecked) { p.student.IsChecked = false; return p.student.Id!=p.student.Id;} return true; });
                LStudentData.FindAll(p => { p.Count = LStudentData.Count; return true; });
                LStudentData.FindAll(p => { p.IsCheckedCount = LStudentData.Count(t => t.student.IsChecked == true); return true; });
                if (LStudentData.Count == 0)
                    Tb_SelectCount.Text = "0";            
                Grid_Data.DataContext = LStudentData;
            }
        }
    
  • 相关阅读:
    20080619 SQL SERVER 输入 NULL 的快捷键
    20090406 Adobe的“此产品的许可已停止工作”错误的解决办法
    20080908 Office Powerpoint 2007 不能输入中文的解决办法
    20080831 ClearGertrude Blog Skin 's cnblogs_code class
    20080603 Facebook 平台正式开放
    20080519 安装 Microsoft SQL Server 2000 时提示 创建挂起的文件操作
    test
    Linux—fork函数学习笔记
    SOA的设计理念
    Why BCP connects to SQL Server instance which start with account of Network Service fail?
  • 原文地址:https://www.cnblogs.com/luluping/p/2057940.html
Copyright © 2011-2022 走看看