0
votes

I use MVVM-Light. In my XAML I have the following binding in a TreeView.

<TreeView Grid.Row="1"
                  ItemsSource="{Binding Model.Root.ChildPages, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}" 

The Model property in the VM looks as follows.

 public TreeModel Model
        {
            get { return model_; }
            set
            {
                Set(nameof(Model), ref model_, value);                   
            }
        }

TreeModel and TreeModel.Root are both ObservableObjects. TreeModel.Root.ChildPages is an ObservableCollection.

(I am actually not changing the currently binded objects, but I set a completely new Model so that I in fact use observable objects as my model should not make any difference but I thought it was worth to mention)

When setting my Model property, my TreeView does not update / showing the new TreeModel. The Set() function from MVVM Light should call RaisePropertyChanged() internally as far as I am aware.

The ViewModel that the View binds to is derived from a MyViewModelBase class which is derived from MVVM Light's ViewModelBase class. The Model property is defined in the MyViewModelBase class.

What am I missing, why does the View not get refreshed? How to solve it?

When setting the Model property from the ViewModels constructor, it will be displayed as expected by the TreeView, it is only successive calls from outside the control to the Model property that does not affect the TreeView. I have debugged and confirmed the line in the setter

 Set(nameof(Model), ref model_, value); 

really gets called.

1
As a side note, you might as well use the overload that omits the property name: Set(ref model_, value);. As to your actual issue, you need to provide more details about how you set the DataContext etc.mm8
Good point thanks. I create a UserControl. In it I have a ContentControl that is bound to a "Mode" property on the UserControls's VM. That "Mode" property will be set to the "ViewModel" discussed in the the question. I use a DataTemplate to set the View that should be connected with the ViewModel. I have a property on the UserControl level. In its setter I post the new model using the MVVM-Light Messenger. The ViewModel discussed in the question receives the message and set the newValue to its own Model property. This property is the code I have in the question.Johan
View's DataContext is retrieved using ViewModelLocator. I use the same ViewModelLocator to set the "Mode" property. So it is a single instance used as DataContext for the View as well used for the DataTemplate in the "outer" UserControl's ContentControl.Johan
I should be able to expect a TreeView to update when ItemSource is set to Model.Root.ChildPages and I update the Model? So it is not due to that I bind to a "sub-property" Do I need to set the ItemSource again somehow? If so how could that be done from the VM? @mm8 Just adding your here so you get noticed about my responses.Johan
You should see the update provided that you set the Model property to a new object. Make sure that you are modifying the data bound instance of the view model.mm8

1 Answers

0
votes

Found the solution to the problem. Dicrionary resources do not get merged the same way when referenced in second level usercontrols as in app.xaml level. Since my view was set to grab the datacontext from the vmlocator and the IoC container was local to the vmlocator (must if one want to be able to create multiple usercontrol), the second level views got new instances of the VMLocator rather than the parent instance. Hence the new instance also injected a new empty model which explained the strange behavior.