• datagrid 绑定选中数据,列头全选


    成品图:

    xaml代码

    <Grid>
            <DataGrid x:Name="datagrid" Height="Auto" Width="Auto" Grid.Row="1"
                      AutoGenerateColumns="False" 
                      CanUserAddRows="False"
                      CanUserDeleteRows="False" 
                      CanUserReorderColumns="False" 
                      CanUserResizeColumns="False" >
                <DataGrid.RowStyle>
                    <Style  TargetType="DataGridRow">
                        <EventSetter Event="GotFocus" Handler="Item_GotFocus"/>
                    </Style>
                </DataGrid.RowStyle>
                <DataGrid.Columns>
                    <DataGridCheckBoxColumn Header="全选" Binding="{Binding IsSelected,Mode=TwoWay}">
                        <DataGridCheckBoxColumn.HeaderTemplate>
                            <DataTemplate>
                                <CheckBox Name="cbCheck" IsChecked="{Binding ElementName=root,Path=CheckboxIsSelected}" Content="选择" Width="60" Click="cbCheck_Click"/>
                            </DataTemplate>
                        </DataGridCheckBoxColumn.HeaderTemplate>
                    </DataGridCheckBoxColumn>
                    <DataGridTextColumn Header="名字" Binding="{Binding Name}"/>
                    <DataGridTextColumn Header="性别" Binding="{Binding Sex}"/>
                    <DataGridTextColumn Header="年龄" Binding="{Binding Age}"/>
                </DataGrid.Columns>
            </DataGrid>
        </Grid>
    

    cs代码

            /// <summary>
            /// 设置数据源
            /// </summary>
            void SetItemsSource()
            {
                List<Person> list = new List<Person>();
                for (int i = 0; i < 10; i++)
                {
                    list.Add(new Person() { Name = "" + i, Age = "1" + i, Sex = i % 3 == 0 ? "男" : "女" });
                }
                datagrid.ItemsSource = list;
            }
            int selectedCount = 0;
            /// <summary>
            /// 绑定列头的勾选框
            /// </summary>
            public bool? CheckboxIsSelected
            {
                get { return (bool?)GetValue(CheckboxIsSelectedProperty); }
                set { SetValue(CheckboxIsSelectedProperty, value); }
            }
            public static readonly DependencyProperty CheckboxIsSelectedProperty =
                DependencyProperty.Register("CheckboxIsSelected", typeof(bool?), typeof(MainWindow), new PropertyMetadata(false));
            /// <summary>
            /// 选中列自动勾选勾选框
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            private void Item_GotFocus(object sender, RoutedEventArgs e)
            {
                var item = (DataGridRow)sender;
                var ss = item.Item as Person;
                if (ss != null)
                {
                    ss.IsSelected = !ss.IsSelected;
                    selectedCount += ss.IsSelected ? 1 : -1;
                    //if(ss.IsSelected)
                    //{
                    //    ss.IsSelected = false;
                    //    selectedCount--;
                    //}
                    //else
                    //{
                    //    ss.IsSelected = true;
                    //    selectedCount++;
                    //}
                }
                CheckboxIsSelected = selectedCount > 0 ? selectedCount == datagrid.Items.Count ? true : (bool?)null : false;
                //if (selectedCount > 0)
                //{
                //    if (selectedCount == datagrid.Items.Count)
                //    {
                //        CheckboxIsSelected = true;
                //    }
                //    else
                //    {
                //        CheckboxIsSelected = null;
                //    }
                //}
                //else
                //{
                //    CheckboxIsSelected = false;
                //}
            }
            /// <summary>
            /// 全选和反选
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            private void cbCheck_Click(object sender, RoutedEventArgs e)
            {
                var items = datagrid.ItemsSource as List<Person>;
                if (items != null)
                {
                    var result = (sender as CheckBox).IsChecked == true ? true : false;
                    items.ForEach(s => s.IsSelected = result);
                }
            }
    

    model代码

    public class Person : ViewModelBase
        {
            private bool _IsSelected;
            /// <summary>
            /// 
            /// </summary>
            public bool IsSelected
            {
                get { return _IsSelected; }
                set
                {
                    _IsSelected = value;
                    RaisePropertyChanged("IsSelected");
                }
            }
            private string _Name;
    
            public string Name
            {
                get { return _Name; }
                set
                {
                    _Name = value;
                    RaisePropertyChanged("Name");
                }
            }
            private string _Age;
    
            public string Age
            {
                get { return _Age; }
                set
                {
                    _Age = value;
                    RaisePropertyChanged("Age");
                }
            }
            private string _Sex;
    
            public string Sex
            {
                get { return _Sex; }
                set
                {
                    _Sex = value;
                    RaisePropertyChanged("Sex");
                }
            }
        }
    

    ViewModelBase

    public class ViewModelBase : INotifyPropertyChanged
        {
            public event PropertyChangedEventHandler PropertyChanged;
            public void RaisePropertyChanged(string propertyName)
            {
                if (this.PropertyChanged != null)
                {
                    this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
                }
            }
        }
  • 相关阅读:
    考驾照的心得
    VS2005+ACCESS WEB程序出错数据访问权限错误的解决方法
    delphi开发回忆录——面向对象的基础,继承(续)
    用人不疑,疑人不用
    delphi开发回忆录——示例源码下载
    delphi开发回忆录——面向对象的基础,继承
    delphi开发回忆录——面向对象的基础,继承(续)
    delphi开发回忆录——示例源码下载
    考驾照的心得
    Win32 API消息函数:GetMessagePos
  • 原文地址:https://www.cnblogs.com/zbfamily/p/9341646.html
Copyright © 2020-2023  润新知