views:

63

answers:

2

Working with the MVVM pattern, I have a pair of view model classes that represent a two-tier data hierarchy, each with a corresponding UserControl that represents its view. Both view model classes implement INotifyPropertyChanged and the root level view model exposes a property that is relevant to both its own view and the child view.

The root level view acquires the root level view model as its data context and explicitly assigns a data context to its contained view. However, it also needs to bind one of the properties of the child view to the above-mentioned shared property. Here is how I have attempted to achieve this, but it's not working:

<UserControl x:Name="rootView">
    <StackPanel>

        <!-- other controls here -->

        <my:ChildView
            DataContext="{Binding Path=SelectedChild}"
            EditingMode="{Binding ElementName=rootView, Path=DataContext.EditingMode />
    </StackPanel>
</UserControl>

Although there are no runtime binding errors and the child view correctly binds to the appropriate child view model instance, its EditingMode property is never set. I have run tests to verify that the corresponding view model property is being modified and that it is notifying this change via INotifyPropertyChanged, but the binding fails to detect it.

Is there a better way to declare this binding or have I made a more basic architectural error?

Many thanks for your advice,

Tim

Update: As requested, I am posting some code to show a very simplified version of my views and view models, together with the results of an experiment that I have conducted that may provide some additional clues.

// The relevant parts of the ParentViewModel class
public class ParentViewModel : INotifyPropertyChanged
{
    // Although not shown, the following properties
    // correctly participate in INotifyPropertyChanged

    public ChildViewModel SelectedChild { get; private set; }

    public ContentEditingMode EditingMode { get; private set; }
}

// The relevant parts of the ChildViewModel class
public class ChildViewModel : INotifyPropertyChanged
{
    // No properties of ChildViewModel affect this issue.
}

// The relevant parts of the ParentView class
public partial class ParentView : UserControl
{
    // No properties of ParentView affect this issue.
}

// The relevant members of the ChildView class
public partial class ChildView : UserControl
{
    public static readonly DependencyProperty EditingModeProperty =
        DependencyProperty.Register(
            "EditingMode",
            typeof(ContentEditingMode),
            typeof(PostView)
        );

    public ContentEditingMode EditingMode
    {
        get { return (ContentEditingMode)GetValue(EditingModeProperty); }
        set { SetValue(EditingModeProperty, value); }
    }
}

// The enumeration used for the EditingMode property
public enum ContentEditingMode
{
    Html,
    WYSYWIG
}

My intention is that the DataContext of the parent view instance will be assigned an instance of ParentViewModel and it will, in turn, assign the value of its SelectedChild property to the DataContext of the nested ChildView. All of this seems to work correctly, but the problem arises because the binding between ParentViewModel.EditingMode and ChildView.EditingMode does not work.

In an attempt to test whether there is a problem with my binding expression, I introduced a TextBlock adjacent to the ChildView and bound it similarly to the ParentViewModel.EditingMode property:

<UserControl x:Name="rootView">
    <StackPanel>

        <!-- other controls here -->

        <TextBlock Text="{Binding ElementName=rootView, Path=DataContext.EditingMode}" />

        <my:ChildView
            DataContext="{Binding Path=SelectedChild}"
            EditingMode="{Binding ElementName=rootView, Path=DataContext.EditingMode />
    </StackPanel>
</UserControl>

In this test, the TextBlock is correctly updated every time the source property changes. However, if I set a breakpoint on the setter of ChildView.EditingMode, it never gets hit.

I'm baffled !

A: 

See if you can just use a full path to get the editing mode of the selected child:

<my:childView
        DataContext="{Binding SelectedChild}"
        EditingMode="{Binding SelectedChild.EditingMode />

HTH,
Berryl

Berryl
Thanks Berryl, but this doesn't work, as the child view model doesn't have the EditingMode property. That's the reason for the post - how to bind the child view to a property on the parent view model.
Tim Coulter
So, SelectedChild is a property of ParentVm and EditingMode is also? That's too easy, because then you would just bind directly to EditingMode. Where do you get EditingMode from if not the RootLevelParentVm? You may need to post the code behind your UserControl.
Berryl
I can't bind directly to the EditingMode property from the child view because the DataContext assigned to the child view is an instance of the child view model, which does not expose this property. As you suggest, I will post a simplified version of my code.
Tim Coulter
Ok - one thing you can do is maintain a reference to the Parent, and set it in the constructor of the Child. I'm guessing the Parent creates the child; if so, it just passes itself in to that child constructor. Now this complexity is in your view models, not the view. Just add whatever Parent properties you need onto the child. For example: EditMode {get{ return _parent.EditMode}}.
Berryl
Hmm - I'm not sure about that. Although I agree that it would solve the problem, the replica child property would need to observe INotifyPropertyChanged, which means that potentially hundreds of events would be raised in child instances every time the parent property changes. Wouldn't this introduce a performance problem?
Tim Coulter
Yes, you need to fire property changed but it would only be on the EditMode of the SelectedChild (which also fires property changed) at any given point in time. No big deal and not a performance hit.
Berryl
+2  A: 

The simplest way to fix this is in your view model. Implement an EditingMode property in the child view model and bind to it. That way, you don't have to make any kind of guesses about what the right way to establish the binding might be; also, it's something that you can test outside of the UI.

Edit

Actually the right solution is not quite as simple, but it's worth knowing how to do.

What you want is for EditingMode in the child control to efficiently inherit its value from the parent control. Does that sound like something that anything else in WPF does? Like just about every framework element that implements dependency properties?

Implement EditingMode as a dependency property in both the parent and child UserControls and use property value inheritance, as described here. That takes the inheritance behavior out of the view model entirely and puts it where it belongs.

Robert Rossney
Thanks for your suggestion Robert. In fact, Berryl made a similar suggestion at almost the same time! Please see my reply comment. Are my concerns about performance justified?
Tim Coulter
Thanks Robert - your edit is exactly the kind of lateral thinking that I need! I will implement it and post again if I hit any obstacles but, in the meantime, I am accepting your answer. Thanks again.
Tim Coulter