WPF Binding to parent DataContext

前端 未结 2 864
谎友^
谎友^ 2021-01-30 16:37

We have a WPF application with a standard MVVM pattern, leveraging Cinch (and therefore MefedMVVM) for View -> ViewModel resolution. This works well, and I can bind the relevant

2条回答
  •  野性不改
    2021-01-30 16:52

    Because of things like this, as a general rule of thumb, I try to avoid as much XAML "trickery" as possible and keep the XAML as dumb and simple as possible and do the rest in the ViewModel (or attached properties or IValueConverters etc. if really necessary).

    If possible I would give the ViewModel of the current DataContext a reference (i.e. property) to the relevant parent ViewModel

    public class ThisViewModel : ViewModelBase
    {
        TypeOfAncestorViewModel Parent { get; set; }
    }
    

    and bind against that directly instead.

提交回复
热议问题