[英]Update an ObservableCollection that is the result of another ObservableCollection
Excuse me but I didn't know how to formulate the question : 对不起,但我不知道如何提出这个问题:
I have two ObservableCollection 我有两个ObservableCollection
ObservableCollection MyMainList is the ObservableCollection where are stored all of my objects. ObservableCollection MyMainList是ObservableCollection,存储了我所有的对象。
ObservableCollection<T> MyFilteredList = new ObservableCollection(MyMainList.Where(x=>Condition))
is the ObservableCollection filtered according some parameters. 是根据某些参数过滤的ObservableCollection。
At one moment, I need to add or remove objects from MyMainList, but then MyFilteredList is not updated automatically. 在一瞬间,我需要从MyMainList中添加或删除对象,但是MyFilteredList不会自动更新。
I need always to do : 我需要经常做:
MyMainList.Add(newObject);
MyFilteredList.Add(newObject);
Is there a way to do all of that in just one line, so when I modify MyMainList, MyFilteredList will be automatically updated? 有没有一种方法可以在一行中完成所有操作,所以当我修改MyMainList时,MyFilteredList将自动更新吗?
Edit : To answer to Adriano Repetti, when I open my window, I load all of objects (so I don't need to reload my DB everytime I change filters). 编辑:要回答Adriano Repetti,当我打开窗口时,我将加载所有对象(因此,每次更改过滤器时都不需要重新加载数据库)。 I have three different filters : - 1 combobox "Affaire" (list of the contracts) - 1 combobox "Phase" (list of the subcontracts) - 1 TextBox "Filter" ( if I want to see only objects whose name contains that text).
我有三种不同的过滤器:-1个组合框“ Affaire”(合同列表)-1个组合框“ Phase”(子合同列表)-1个文本框“过滤器”(如果我只想查看名称包含该文本的对象) 。
When I change selection of 1st ComboBox, I update the list of subcontracts, and update the filtered list of assemblies. 当我更改第一个ComboBox的选择时,我会更新分包合同列表,并更新已过滤的程序集列表。
private Affaire selectedAffaire;
public Affaire SelectedAffaire
{
get { return selectedAffaire; }
set
{
selectedAffaire = value;
this.NotifyPropertyChanged("SelectedAffaire");
if (value != null)
{
GetListPhaseInContract(); //I update the list of subcontracts in 2nd Combobox
}
UpdateListAssemblages(); // I update MyFilteredList
}
}
When I change selection of 2nd ComboBox (subcontract), I update the list of MyFilteredList 当我更改第二个ComboBox(转包)的选择时,我更新了MyFilteredList的列表
private Phase selectedPhase;
public Phase SelectedPhase
{
get { return selectedPhase; }
set
{
selectedPhase = value;
this.NotifyPropertyChanged("SelectedPhase");
UpdateListAssemblages();
}
}
Then when I change my TextBox value, I also update the list 然后,当我更改TextBox值时,我也会更新列表
private string texteFiltre;
public string TexteFiltre
{
get { return texteFiltre; }
set
{
texteFiltre = value;
this.NotifyPropertyChanged("TexteFiltre");
UpdateListAssemblages();
}
}
If I understood, this post is what I need to look for? 如果我理解,这是我需要查找的帖子 ? I don't know yet what are ICollectionView, but I guess I need to look in that way?Will I gain some execution time using ICollectionView?
我还不知道什么是ICollectionView,但是我想我应该这样看,使用ICollectionView会得到一些执行时间吗? as I see I need anyway to Refresh it?
如我所见,我仍然需要刷新它?
Since you didn't give us the detial of the data struct, I just assume that PhaseInContract
is included in MainList
. 由于您没有给我们提供数据结构的详细信息,因此我仅假设
PhaseInContract
已包含在MainList
。 So, just simply create two views for your MainList
, then binding them to UI. 因此,只需为
MainList
创建两个视图,然后将它们绑定到UI。 All views will synchronize with source automatically. 所有视图将自动与源同步。
Public ICollectionView MainListView;
Public ICollectionView PhaseInContractView;
Public ObservableCollection<YourDataClass> MainList;
public YourViewModel()
{
MainList = new ObservableCollection<YourDataClass>();
// Load datas form db and fill MainList
MainListView = new CollectionViewSource() { Source = MainList }.View;
MainListView.Filter = (x) =>
{
// your MainListView filtering logic.
};
PhaseInContractView = new CollectionViewSource() { Source = MainList }.View;
PhaseInContractView.Filter = (x) =>
{
// your PhaseInContractView filtering logic
};
private Affaire selectedAffaire;
public Affaire SelectedAffaire
{
get { return selectedAffaire; }
set
{
selectedAffaire = value;
this.NotifyPropertyChanged("SelectedAffaire");
if (value != null)
{
PhaseInContractView.Refresh();
}
MainListView.Refresh();
}
}
// And other properties.
...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.