0

I'm am currently working on a WPF/XAML project where i have the following problem:

I have a ObservableCollection which fetches it's data from a model as soon as the program starts and here is the deal.

How can i make another ObservableCollection which updates it's data on behalf of what you've chosen in the first ObservableCollection?

ρяσѕρєя K
  • 132,198
  • 53
  • 198
  • 213

2 Answers2

1

Subscribing to the CollectionChanged event and recreating the ObservableCollection should work:

public readonly ObservableCollection<string> Collection1 =
        new ObservableCollection<string>();

public readonly ObservableCollection<string> Collection2 =
        new ObservableCollection<string>();

public ViewModel() {
    Collection1.CollectionChanged += (sender, args) =>
    {
        Collection2.Clear();
        foreach (var x in Collection1) {
            Collection2.Add(x);
        }
    };
}
Peter
  • 674
  • 6
  • 14
  • Hi @TaylorDrift, if this or any answer has solved your question please consider [accepting it](http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work) by clicking the check-mark. This indicates to the wider community that you've found a solution and gives some reputation points to both the answerer and yourself. There is no obligation to do this. – Peter Jan 10 '17 at 15:58
0
  1. You should look into ObservableCollection events. MSDN
  2. it is not recommended to use ObservableCollection directly, a more straightforward way to use ObservableCollection capabilities from XAML in an application is to declare your own non-generic custom collection class that derives from ObservableCollection, and constrains it to a specific type
  3. Examples can be found here and here
Community
  • 1
  • 1
Maxim Kitsenko
  • 2,042
  • 1
  • 20
  • 43