gpt4 book ai didi

c# - 选中数据网格标题中的所有复选框及其在 wpf mvvm 中的绑定(bind)

转载 作者:行者123 更新时间:2023-12-03 10:21:27 27 4
gpt4 key购买 nike

我正在研究 wpf(mvvm 架构)。我正在使用数据网格在标题中列出复选框和 SELECT ALL 复选框,单击我希望选中所有复选框,反之亦然。请帮忙。

我在这里给出我的代码描述......
这是 View.xaml

<DataGridCheckBoxColumn Binding="{Binding IsSelected}" Width="50" >
<DataGridCheckBoxColumn.HeaderTemplate>
<DataTemplate x:Name="dtAllChkBx">
<CheckBox Name="cbxAll" Content="All" IsChecked="{Binding Path=DataContext.AllSelected,RelativeSource={RelativeSource AncestorType=DataGrid}}"/>
</DataTemplate>
</DataGridCheckBoxColumn.HeaderTemplate>
</DataGridCheckBoxColumn>
<DataGridTemplateColumn Header="Name" Width="SizeToCells" IsReadOnly="True">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding UsecaseName}" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>

下面是 View 模型类
private bool _IsSelected;
public bool IsSelected
{
get { return _IsSelected; }
set
{
_IsSelected = value;
OnPropertyChanged("IsSelected");
}
}

private bool _AllSelected;
public bool AllSelected
{
get { return _AllSelected; }
set
{
_AllSelected = value;
foreach (var reportListItemModel in UsecaseListItems)
{
reportListItemModel.IsSelected = this._AllSelected;
}
OnPropertyChanged("IsSelected");

}
}

private ObservableCollection<UseCase> _usecaseListItems = new ObservableCollection<UseCase>();
public ObservableCollection<UseCase> UsecaseListItems
{
get { return _usecaseListItems; }
set {
_usecaseListItems = value;
OnPropertyChanged("UsecaseListItems");
}
}

公共(public)类用例:BaseNotifyPropertyChanged
{
公共(public)字符串 UsecaseName { 获取;放; }
    public bool IsSelected { get; set; }

}

还应该做什么才能使全选复选框正常工作?

最佳答案

你的 UseCase 类必须实现 INotifyPropertyChanged 界面

public class UseCase : INotifyPropertyChanged
{
//...
private bool _isSelected;
public bool IsSelected
{
get
{
return _isSelected;
}
set
{
_isSelected = value;
NotifyPropertyChanged("IsSelected");
}
}
public event PropertyChangedEventHandler PropertyChanged;
protected void NotifyPropertyChanged(String info)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(info));
}
}
}

编辑 :在这里我得到了一个运行良好的完整示例

*.xaml(查看)
<DataGrid x:Name="dataGrid" ItemsSource="{Binding MyCollection}" AutoGenerateColumns="False">
<DataGrid.Columns>
<DataGridCheckBoxColumn Binding="{Binding IsSelected}" Width="50" >
<DataGridCheckBoxColumn.HeaderTemplate>
<DataTemplate x:Name="dtAllChkBx">
<CheckBox Name="cbxAll" DataContext="{Binding ElementName=dataGrid, Path=DataContext}" Command="{Binding MyCommand}" CommandParameter="{Binding Path=IsChecked, RelativeSource={RelativeSource Self}}"/>
</DataTemplate>
</DataGridCheckBoxColumn.HeaderTemplate>
</DataGridCheckBoxColumn>
<DataGridTemplateColumn Header="Name" Width="SizeToCells" IsReadOnly="True">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding UsecaseName}" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>

*.cs( View 模型)
public class MainViewModel : INotifyPropertyChanged
{
public MainViewModel()
{
for (int i = 0; i < 100; i++)
{
MyCollection.Add(new UseCase { UsecaseName = "name " + i, IsSelected = false });
}
MyCommand = new RelayCommand(MyCommandAction);
}


private ObservableCollection<UseCase> myCollection = new ObservableCollection<UseCase>();

public ObservableCollection<UseCase> MyCollection
{
get
{
return myCollection;
}
set
{
myCollection = value;
NotifyPropertyChanged("MyCollection");
}
}

public RelayCommand MyCommand { get; set; }
private void MyCommandAction(object obj)
{
foreach (var item in MyCollection)
{
item.IsSelected = (bool)obj;
}
}



//NotifyPropertyChanged
public event PropertyChangedEventHandler PropertyChanged;
protected void NotifyPropertyChanged(String info)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(info));
}
}
}

*.cs(型号)
public class UseCase : INotifyPropertyChanged
{
public string UsecaseName { get; set; }
private bool _isSelected;
public bool IsSelected
{
get
{
return _isSelected;
}
set
{
_isSelected = value;
NotifyPropertyChanged("IsSelected");
}
}
public event PropertyChangedEventHandler PropertyChanged;
protected void NotifyPropertyChanged(String info)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(info));
}
}
}

关于c# - 选中数据网格标题中的所有复选框及其在 wpf mvvm 中的绑定(bind),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37029133/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com