Datacontext binding problem

Oct 19, 2011 at 1:25 PM

I am currently testing with the Catel example WPF MasterDetail project, but I ran into a problem for which can't seem to find a solution.

In the provided example the PersonMasterViewModel includes a list of Persons and a SelectPerson property. After the list is filled the first person in this list is selected. In this situation the the view is correctly updated when an item is selected from the list. It is also possible to set the SelectedPerson to null after the model is initialized this way. The PersonDetailView will then be empty.

However if I change the implementation slightly and do not initialize the SelectedPerson and it remains null then the view does not get updated when I select a person from the list.

Am I missing something?

Oct 19, 2011 at 2:18 PM

The SelectedPerson must be set to a value. If the SelectedItem is bound to the ListBox, the listbox should update the binding on the view model. Is the only thing you changed the default selection code line?

Oct 19, 2011 at 4:14 PM

We can reproduce this issue, we will investigate this. Thanks for letting us know.

Oct 19, 2011 at 9:40 PM

Found the issue, we are currently working on a fix. I think we can submit this tomorrow evening or on friday.

Oct 20, 2011 at 2:08 PM
Edited Oct 20, 2011 at 2:08 PM

Thank you for your quick response. I'm glad you found the problem and I am looking forward to your fix for this issue.

Oct 20, 2011 at 6:34 PM

I have committed the fix. You can either download the code from this changeset, or download the latest beta:

Oct 21, 2011 at 2:34 PM

I've tested the lastest beta and can confirm that the reported issue is fixed. However I ran into another problem. It seems nested view/viewmodel bindings don't work.

To reproduce the problem you can modify the MasterDetail project using the following steps:

  1. Make the Person property of the PersonDetailViewModel public.
  2. Copy the PersonDetailView and rename it to Person2DetailView.
  3. Add the Person2DetailView to the PersonDetailView and bind the Datacontext to the Person property which  was made public.

My modified PersonDetailView:


<catel:UserControl x:Class="Catel.Examples.WPF.MasterDetail.Views.PersonDetailView">
        <!-- Last name -->
        <Label Content="Last name" />
        <TextBox Text="{Binding LastName, ValidatesOnDataErrors=True, NotifyOnValidationError=True}" />
        <Views:Person2DetailView Grid.ColumnSpan="2" DataContext="{Binding Person}"/>


What I expect is to see when I run the example is both views showing the content of the PersonDetailViewModel. However only the original view shows the person information and the Person2DetailView remains empty.

Oct 21, 2011 at 3:43 PM

Works like a charm here. This is what I have done:

  1. Make person public
  2. Copy PersonDetailView, and renamed the class (both in the code-behind as the xaml)
  3. Added this to the PersonDetailView (the original one):

<views:PersonDetailView2 Grid.Column="2" DataContext="{Binding Person}" />


I now see 2 detail views of the same person. If I change the name in one of the views, all other views (both master as other detail view) update.

Oct 21, 2011 at 4:08 PM

It appears I missed one crucial step in my reproduce scenario.

  1. Make the initially selected person null.
  2. Other steps are the same.

It appears this problem is closely related to my initial problem. It works fine when the initially selected person set, but not when it is null.

Oct 21, 2011 at 6:08 PM

Thanks, I will investigate this and let you know.

Oct 22, 2011 at 12:30 PM

The control doesn't even get a DataContext change event, so this is really weird. First I thought the Person property was simply not available, but binding to Person.FirstName in PersonDetailView works. I don't understand why WPF doesn't raise a DataContextChanged event because the DataContext actually changes.

I will try to figure this out, but for now it seems to be out of control and in the WPF system itself.

Oct 22, 2011 at 6:00 PM

I finally found the issue, it was an issue in the UserControl<TViewModel> that prevented a datacontext change in case the initial value was null. I have updated the beta, you can now download the fix here:

If you have any other issues, just let us know!