mvvm绑定checkbox wpf_WPF的DataGrid TemplateColumn中使用复选框,并选择在头所有-checkbox使用MVVM光...

这篇博客探讨了如何在WPF DataGrid中使用MVVM模式实现复选框列,并添加一个头部复选框来实现全选/全不选功能。作者遇到了问题,即头部复选框的点击无法同步更新数据行中的复选框状态,尽管模型中的IsSelected属性已正确更新。解决方案是确保ReportListItemModel实现了INotifyPropertyChanged并触发PropertyChanged事件。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

i'm trying to create a DataGrid with a column of checkboxes. On that CheckBox-column's header, i'd like to have a "check/uncheck all"-type of checkbox.

App is using WPF, .NET 4 and MVVM Light.

Here's the code:

XAML:

...

ViewModel:

private ObservableCollection _reportListItems;

public ObservableCollection ReportListItems

{

get

{

return this._reportListItems;

}

private set

{

this._reportListItems = value;

this.RaisePropertyChanged("ReportListItems");

}

}

....

public bool CheckAll

{

get { return this._checkall; }

set

{

this._checkall = value;

foreach (var reportListItemModel in ReportListItems)

{

reportListItemModel.IsSelected = this._checkall;

}

}

}

Model:

public class ReportListItemModel

{

public EnvironmentConfigurationModel TargetConfigurationModel { get; set; }

public ReportModel TargetReport { get; set; }

public EnvironmentConfigurationModel SourceConfigurationModel { get; set; }

public ReportModel SourceReport { get; set; }

private bool _isSelected;

public bool IsSelected

{

get { return _isSelected; }

set

{

_isSelected = value;

}

}

}

My idea is to bind the DataGrid to a ObservableCollection of type ReportListItemModel. ReportListItemModel contains a public boolean property "IsSelected", which i want to get bound to the checkbox.

Scenario:

As a user, i want to be able to select (or deselect) all the rows by clicking on the checkbox located at the header row.

Tests:

Task: Click the "Header-Checkbox", when it's state is unchecked.

Expected: All checkboxes on individual rows get checked.

Actual: Only the checkbox on header row gets checked.

Task: Click the "Header-Checkbox", when it's state is checked.

Expected: All checkboxes on individual rows get unchecked.

Actual: Only the checkbox on header row gets unchecked.

Both the "select all"-checkbox and the checboxes on the rows cause the property within model to be set as expected. This just doesn't get bound to the View. I have a creepy feeling that i have my model and viewmodel set funny somehow, even though the DataGridTextColumn does get the value from model a-ok.

I'm happy to provide any additional code or info required!

N.B. I'm just starting up with WPF, MVVM etc., so if there's some fundamental flaw here, i'd be happy to hear about it.

Edit:

Edited for readability...

解决方案

If you have derived ReportListItemModel from INotifyPropertyChanged then raise property changed event from IsSelected property setter: RaisePropertyChange("IsSelected").

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值