如何解决WPF DataGrid TemplateColumn,带有CheckBoxes,并使用MVVM Light在标题中选择All -checkbox
| 我正在尝试创建带有一列复选框的DataGrid。在该CheckBox列的标题上,我想要一个\“ check / uncheck all \”类型的复选框。 应用正在使用WPF,.NET 4和MVVM Light。 这是代码: XAML:<DataGrid x:Name=\"dgReportList\" ItemsSource=\"{Binding Path=ReportListItems}\">
<DataGrid.Columns>
<DataGridTextColumn Header=\"*SomeText\" Binding=\"{Binding Path=SourceReport.Name}\" />
<DataGridTemplateColumn>
<DataGridTemplateColumn.Header>
<CheckBox IsChecked=\"{Binding Source={StaticResource Locator},Path=MainWindowviewmodel.CheckAll,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}\" />
</DataGridTemplateColumn.Header>
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<CheckBox IsChecked=\"{Binding Path=IsSelected,UpdateSourceTrigger=PropertyChanged}\">
</CheckBox>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
...
viewmodel:
private ObservableCollection<ReportListItemmodel> _reportListItems;
public ObservableCollection<ReportListItemmodel> 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;
}
}
}
模型:
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;
}
}
}
我的想法是将DataGrid绑定到类型为ReportListItemmodel的ObservableCollection。 ReportListItemmodel包含一个公共布尔属性\“ IsSelected \”,我希望将其绑定到该复选框。
场景:
作为用户,我希望能够通过单击位于标题行的复选框来选择(或取消选择)所有行。
测试:
任务:未选中状态下的\“ Header-CheckBox \”。
预期:单个行上的所有复选框都被选中。
实际:仅选中标题行上的复选框。
任务:选中状态后,单击\“ Header-CheckBox \”。
预期:单个行上的所有复选框都未选中。
实际:仅选中标题行上的复选框。
行中的“全选”复选框和复选框均使模型内的属性按预期设置。这只是不绑定到视图。我有一种令人毛骨悚然的感觉,尽管DataGridTextColumn确实从正常的模型中获取了值,但我还是以某种方式将我的模型和视图模型设置为有趣的。
我很乐意提供所需的任何其他代码或信息!
N.B.我只是从WPF,MVVM等开始,所以如果这里存在一些基本缺陷,我将很高兴听到它。
编辑:
编辑以提高可读性...
解决方法
如果您从INotifyPropertyChanged派生了
ReportListItemModel
,则从IsSelected
属性设置器RaisePropertyChange(\"IsSelected\")
引发属性更改事件。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。