0

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();
2
  • 1
    can you add the xaml you use to bind to this list? I'm going to assume you have a property in your MainViewModel that you set to = this categoryViewModel? The code you have shown is creating a new CategoryViewModel and setting it's CategoryList to values from the file. If you aren't setting the MainViewModels CategoryViewModel property to = this new one, that would be the problem. Commented Mar 14, 2014 at 16:05
  • 1
    Have you double checked that the Category view model you update in the Main Window View Model is the same instance that is bound to the UI of your user control. Commented Mar 14, 2014 at 16:15

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.