I have a DataGrid
binding to a List<T>
. Below is my class. The List<FSCLevel>
is binding to the RowDetails
in my main DataGrid
. I have added a filter control for users to filter through CUSTOMER_CODE
.
我有一个DataGrid绑定到
public class CUSTCARDLevel: INotifyPropertyChanged
{
#region Members
private string _cUSTOMER_CODE;
private List<FSCLevel> _fSClist;
#endregion
#region Properties
public string CUSTOMER_CODE { get { return _cUSTOMER_CODE; } set { _cUSTOMER_CODE = value; OnPropertyChanged("CUSTOMER_CODE"); } }
public List<FSCLevel> FSCLIST { get { return _fSClist; } set {_fSClist = value; OnPropertyChanged("FSCLIST"); } }
#endregion
// INotifyPropertyChanged interface
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{ PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}
I now need to apply some filters on the list, e.g. on DataGrid
it only shows CUSTOMER_CODE
is "CUST1".
我现在需要在列表上应用一些过滤器,例如在DataGrid上它只显示CUSTOMER_CODE是“CUST1”。
So here is the question, do I have to use an ObservableCollection
for the filter purpose? Is there a way that I can just use the class with INotifyPropertyChanged
interface? If so, what is the best way to handle the List<T1>
within a List<T>
? Say I cast my list into ObservableCollection<T>
using:
这里有一个问题,我是否必须使用一个天文台来进行过滤?有没有一种方法可以使用INotifyPropertyChanged接口的类?如果是,在列表
ObservableCollection<CUSTCARDLevel> myObserveList = new ObservableCollection<CUSTCARDLevel>(myList);
What is going to happen to the List<FSCLevel>
inside? or how do I cast List<FSCLevel>
inside CUSTCARDLevel into ObservableCollection? Or I need to do something to fix my class?
列表
1 个解决方案
#1
0
I wanted to put this as a comment but it went off the character limit. I'll get right to it.
我想把它作为一个评论,但它超出了字符限制。我马上就去做。
Well it's all about the need of the hour.
这都是关于时间的需要。
I would strongly recommend against the frequent casting using
new ObservableCollection<object>(myList)
since it'll each time return a new instance that'll take upmemory
.我强烈建议不要频繁使用新的ObservableCollection
Now let's assume you have a List<T>
and any changes in the items of the List<T>
you want to reflect on the UI then you should only use an ObservableCollection
for the List<T>
and not for any List
that are a part of Class T
.
现在,假设您有一个列表
On the contrary, if you have a UI that needs to be updated each time the collection
in the class T
is modified, I would recommend that you make it an ObservableCollection
.
相反,如果您有一个UI,每次修改类T中的集合时都需要更新它,那么我建议您将它设置为一个ObservableCollection。
Generally in the most common ways, Developers use ExtensionMethods
to convert a List<T>
to an ObservableCollection<T>
Read more about it here
通常,在最常见的方法中,开发人员使用extensionmethod将列表
Also, they use XAML Converters
and bind the element directly to the list.
此外,他们使用XAML转换器并将元素直接绑定到列表中。
My final recommendation: Use an observable collection based on your needs as explained above but most importantly, don't operate on
Lists
unless it's actually necessary. Fetch thedata
from your service into aList
convert it once into anObservableCollection
and then useSystem.LINQ
to filter the data from it.我最后的建议是:根据您的需要使用可观察的集合,如上面所述,但最重要的是,除非确实需要,否则不要对列表进行操作。从您的服务中获取数据到列表中,将其转换为一个ObservableCollection,然后使用System。LINQ来过滤数据。
#1
0
I wanted to put this as a comment but it went off the character limit. I'll get right to it.
我想把它作为一个评论,但它超出了字符限制。我马上就去做。
Well it's all about the need of the hour.
这都是关于时间的需要。
I would strongly recommend against the frequent casting using
new ObservableCollection<object>(myList)
since it'll each time return a new instance that'll take upmemory
.我强烈建议不要频繁使用新的ObservableCollection
Now let's assume you have a List<T>
and any changes in the items of the List<T>
you want to reflect on the UI then you should only use an ObservableCollection
for the List<T>
and not for any List
that are a part of Class T
.
现在,假设您有一个列表
On the contrary, if you have a UI that needs to be updated each time the collection
in the class T
is modified, I would recommend that you make it an ObservableCollection
.
相反,如果您有一个UI,每次修改类T中的集合时都需要更新它,那么我建议您将它设置为一个ObservableCollection。
Generally in the most common ways, Developers use ExtensionMethods
to convert a List<T>
to an ObservableCollection<T>
Read more about it here
通常,在最常见的方法中,开发人员使用extensionmethod将列表
Also, they use XAML Converters
and bind the element directly to the list.
此外,他们使用XAML转换器并将元素直接绑定到列表中。
My final recommendation: Use an observable collection based on your needs as explained above but most importantly, don't operate on
Lists
unless it's actually necessary. Fetch thedata
from your service into aList
convert it once into anObservableCollection
and then useSystem.LINQ
to filter the data from it.我最后的建议是:根据您的需要使用可观察的集合,如上面所述,但最重要的是,除非确实需要,否则不要对列表进行操作。从您的服务中获取数据到列表中,将其转换为一个ObservableCollection,然后使用System。LINQ来过滤数据。