wpf中为DataGrid添加checkbox支持多选全选

项目中用到DataGrid, 需要在第一列添加checkbox, 可以多选、全选。
其中涉及的概念DataTemplate, DataGridCellStyle, DataGridCellControlTemplate,Binding, OnPropertyChanged等。

有下面是实现思路:

1.继承INotifyPropertyChanged接口,实现OnPropertyChanged方法:
 public abstract class ViewModelBase : INotifyPropertyChanged

    {

      public event PropertyChangedEventHandler PropertyChanged;

        /// <summary>
        /// Raises this object&apos;s PropertyChanged event.
        /// </summary>
        /// <param name="propertyName">The property that has a new value</param>
        protected void OnPropertyChanged(string propertyName)
        {
            PropertyChangedEventHandler handler = PropertyChanged;

            if (null != handler)
            {
                handler(this, new PropertyChangedEventArgs(propertyName));
            }
        }

//..................

}

2. 实现viewModel, 添加IsSelected属性, 存储当前多选状态
        private bool _isSelected = true;

        public bool IsSelected
        {
            get { return _isSelected; }

            set
            {
                _isSelected = value;
                OnPropertyChanged("IsSelected");
            }
        }

3.在VM/Model准备好后, 我们接下来开始对DataGrid进行style自定义

4.准备checkbox的DataTemplate:

<DataTemplate x:Key="CheckboxDataTemplate1">
          <Grid>
          <CheckBox x:Name="_chkSelected"
                        Height="16"
                        HorizontalAlignment="Center"
                        VerticalAlignment&
  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
好的,以下是示例代码: XAML: ``` <ListBox ItemsSource="{Binding Items}"> <ListBox.ItemTemplate> <DataTemplate> <StackPanel Orientation="Horizontal"> <CheckBox IsChecked="{Binding IsSelected}" /> <TextBlock Text="{Binding Name}" /> </StackPanel> </DataTemplate> </ListBox.ItemTemplate> </ListBox> ``` ViewModel: ``` public class MainViewModel : INotifyPropertyChanged { public ObservableCollection<ItemViewModel> Items { get; set; } public MainViewModel() { Items = new ObservableCollection<ItemViewModel> { new ItemViewModel { Name = "Item 1" }, new ItemViewModel { Name = "Item 2" }, new ItemViewModel { Name = "Item 3" }, new ItemViewModel { Name = "Item 4" }, new ItemViewModel { Name = "Item 5" } }; } public event PropertyChangedEventHandler PropertyChanged; protected void OnPropertyChanged([CallerMemberName] string propertyName = null) { PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); } } public class ItemViewModel : INotifyPropertyChanged { private bool _isSelected; public string Name { get; set; } public bool IsSelected { get { return _isSelected; } set { _isSelected = value; OnPropertyChanged(); } } public event PropertyChangedEventHandler PropertyChanged; protected void OnPropertyChanged([CallerMemberName] string propertyName = null) { PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); } } ``` 在 ViewModel 中,我们使用了 INotifyPropertyChanged 接口来通知 View 层数据的变化。同时,我们将每个 Item 的 IsSelected 属性绑定到了 CheckBox 的 IsChecked 属性上,这样就可以实现多选了。最后,我们可以在 ViewModel 中获取选中的 Item,从而将选中的值传入后台。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值