您的位置:首页 > 科技 > IT业 > WPF中DataGrid实现多选框功能

WPF中DataGrid实现多选框功能

2024/12/27 2:29:41 来源:https://blog.csdn.net/abc1564984930/article/details/139135072  浏览:    关键词:WPF中DataGrid实现多选框功能

1. 效果图

2. Model建立

public class RstModelCheck : ObservableObject
{//为了显示Head1和Head2.而且View中绑定属性而非字段,否则不能显示。public string? Name { get; set; }    public bool PlatenAll {get => _platenAll;set{SetProperty(ref _platenAll, value);PlatenA = value;PlatenB = value;PlatenC = value;PlatenD = value;}}public bool PlatenA {get => _platenA;set => SetProperty(ref _platenA, value);}public bool PlatenB{get => _platenB;set => SetProperty(ref _platenB, value);}public bool PlatenC{get => _platenC;set => SetProperty(ref _platenC, value);}public bool PlatenD{get => _platenD;set => SetProperty(ref _platenD, value);}private bool _platenAll;private bool _platenA;private bool _platenB;private bool _platenC;private bool _platenD;public RstModelCheck(string name){this.Name = name;}
}

3. ViewModel编写

public ObservableCollection<RstModelCheck> IsCheckRstModel
{get => _isCheckRstModel;set => SetProperty(ref _isCheckRstModel, value);
}
private ObservableCollection<RstModelCheck> _isCheckRstModel = new();//构造函数中添加:
IsCheckRstModel.Add(new RstModelCheck("Head1"));
IsCheckRstModel.Add(new RstModelCheck("Head2"));

4. View中Xaml编写

<DataGrid ItemsSource="{Binding IsCheckRstModel}" AutoGenerateColumns="False" SelectionUnit="Cell" ColumnHeaderHeight="20"><DataGrid.Columns><DataGridTextColumn Header="Platen" Binding="{Binding Name}" IsReadOnly="True" CanUserSort="False" Selector.IsSelected="False"/><DataGridCheckBoxColumn Header="AllSet" Binding="{Binding Path=PlatenAll,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" CanUserSort="False"/><DataGridCheckBoxColumn Header="A" Binding="{Binding PlatenA}" CanUserSort="False"/><DataGridCheckBoxColumn Header="B" Binding="{Binding PlatenB}" CanUserSort="False"/><DataGridCheckBoxColumn Header="C" Binding="{Binding PlatenC}" CanUserSort="False"/><DataGridCheckBoxColumn Header="D" Binding="{Binding PlatenD}" CanUserSort="False"/></DataGrid.Columns>
</DataGrid>

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com