Reputation: 95
In my project I want to display a List on a user control. For that I have a CategoryView the user control with an ListView-control, where I want to display the List. And the CategoryViewModel. On the ViewModel I have a list - property where I also raise the property changed event.
public class CategoryViewModel : NotificationObject
{
private List<string> categoryList;
public List<string> CategoryList
{
get
{
return this.categoryList;
}
set
{
this.categoryList = value;
this.RaisePropertyChanged("CategoryList");
}
}
}
This List is binded to the ListView-element in the view.
If I change the List in the CategoryViewModel, it works fine and the property change event is raised. If I change the List from the MainWindowViewModel. No property Changed event is Raised and the View will not be updated. How do I have to do that?
On the MainWindowViewModel I change the CategoryList. The List will be filled correctly.
CategoryViewModel categoryViewModel = new CategoryViewModel();
categoryViewModel.CategoryList = logger.ReadLogfile(this.logFileName).ToList();
Upvotes: 0
Views: 1173
Reputation: 69959
You seem to be somewhat confused. You have one ListView
in your CategoryView UserControl
. Its ItemsSource
property can only be data bound to one collection, so clearly, when changing the collections in the main view model and the CategoryViewModel
, only one will affect the ListView
.
It seems from your code that the CategoryViewModel
is set as the DataContext
for the UserControl
, so the collection in the main view model will not be connected to the ListView
. If you want to data bind from the ListView
to the collection in the main view model instead, then you'll need to use a RelativeSource Binding
instead:
<ListView ItemsSource="{Binding SomeCollection, RelativeSource={RelativeSource
AncestorType={x:Type YourPrefix:YourParentType}}}" ... />
Even so, now the collection in your CategoryViewModel
will no longer be connected, so you'd better decide exactly what you want to do here.
Upvotes: 1