Reputation: 1595
Excuse me but I didn't know how to formulate the question :
I have two ObservableCollection
ObservableCollection MyMainList is the ObservableCollection where are stored all of my objects.
ObservableCollection<T> MyFilteredList = new ObservableCollection(MyMainList.Where(x=>Condition))
is the ObservableCollection filtered according some parameters.
At one moment, I need to add or remove objects from MyMainList, but then MyFilteredList is not updated automatically.
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?
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). 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).
When I change selection of 1st ComboBox, I update the list of subcontracts, and update the filtered list of assemblies.
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
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
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? as I see I need anyway to Refresh it?
Upvotes: 0
Views: 669
Reputation: 1883
Since you didn't give us the detial of the data struct, I just assume that PhaseInContract
is included in MainList
. So, just simply create two views for your MainList
, then binding them to 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.
...
Upvotes: 1