简体   繁体   中英

WPF + MVVM - How to bind a property to the parent view's data context

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

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 !

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 />

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 UserControl s 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM