WPF C# 如何 select 在两个集合之间基于活动按钮在 GridView 中使用
WPF C# How to select between two collection to use in a GridView based on buttons active
我希望能够根据激活的单选按钮将两个不同的集合 NewItems 和 OldItems QueryAbleCollection 绑定为 ItemsSource,
假设我们有一个名为 NewItems 和 OldItems 的按钮,每个按钮都应更改 GridView 中的 Itemssource 应使用的集合。
如何使用 XAML 和 C# ViewModel 以最简单的方式实现此目的?
下面是我的尝试,但我不确定是否最明智的做法是为 SelectedCollection 分配另一个集合。如果这是一个好方法或建议更好的方法,有人可以纠正我吗?现在我有两个按钮,它们刚刚触发了 true 和 false 之间的 isOldItems。
private bool isOldItems;
public bool isOldItems
{
get
{
return this.isOldItems;
}
set
{
this.isOldItems= value;
this.OnPropertyChanged("isOldItems");
this.OnPropertyChanged("SelectedCollection");
}
}
private QueryableCollectionView _selectedCollection;
public QueryableCollectionView SelectedCollection
{
get
{
if (isOldItems== true)
return SelectedCollection= this.OldItemsCollection;
else
return SelectedCollection= this.NewItemsCollection;
}
set
{
if (this._selectedCollection!= value)
{
this._selectedCollection= value;
this.OnPropertyChanged("SelectedCollection");
}
}
}
如果这些是同一类型的集合,我认为您所做的事情没有明显的巨大缺点。无论如何从广义上讲。
这是您要设置的引用,因此如果您将 SelectedCollection 设置为 NewItemsCollection,则它指向 NewItemsCollection。将项目添加到 SelectedCollection,然后添加到 NewItemsCollection(或设置为的任何一个)。
但是
您拥有的 if 检查将意味着它永远不会设置支持变量并从 setter 引发 propertychanged。
相同类型的对象总是相等的,除非你覆盖等于。
你的
if (this._selectedCollection!= value)
不会检查集合中的每个项目并查看它们是否不同,或者您设置的名称。它会做的是比较类型。这些大概总是相等的。
删除 setter。
我希望能够根据激活的单选按钮将两个不同的集合 NewItems 和 OldItems QueryAbleCollection 绑定为 ItemsSource,
假设我们有一个名为 NewItems 和 OldItems 的按钮,每个按钮都应更改 GridView 中的 Itemssource 应使用的集合。
如何使用 XAML 和 C# ViewModel 以最简单的方式实现此目的?
下面是我的尝试,但我不确定是否最明智的做法是为 SelectedCollection 分配另一个集合。如果这是一个好方法或建议更好的方法,有人可以纠正我吗?现在我有两个按钮,它们刚刚触发了 true 和 false 之间的 isOldItems。
private bool isOldItems;
public bool isOldItems
{
get
{
return this.isOldItems;
}
set
{
this.isOldItems= value;
this.OnPropertyChanged("isOldItems");
this.OnPropertyChanged("SelectedCollection");
}
}
private QueryableCollectionView _selectedCollection;
public QueryableCollectionView SelectedCollection
{
get
{
if (isOldItems== true)
return SelectedCollection= this.OldItemsCollection;
else
return SelectedCollection= this.NewItemsCollection;
}
set
{
if (this._selectedCollection!= value)
{
this._selectedCollection= value;
this.OnPropertyChanged("SelectedCollection");
}
}
}
如果这些是同一类型的集合,我认为您所做的事情没有明显的巨大缺点。无论如何从广义上讲。
这是您要设置的引用,因此如果您将 SelectedCollection 设置为 NewItemsCollection,则它指向 NewItemsCollection。将项目添加到 SelectedCollection,然后添加到 NewItemsCollection(或设置为的任何一个)。
但是 您拥有的 if 检查将意味着它永远不会设置支持变量并从 setter 引发 propertychanged。 相同类型的对象总是相等的,除非你覆盖等于。 你的
if (this._selectedCollection!= value)
不会检查集合中的每个项目并查看它们是否不同,或者您设置的名称。它会做的是比较类型。这些大概总是相等的。
删除 setter。