【发布时间】:2016-07-21 20:00:38
【问题描述】:
MainWindow 绑定到MainViewModel,其余的都绑定到ChildViewModel,这是MainViewModel 的一个属性。现在在绑定到ChildViewModel 的最里面的子窗口中,我想将一个属性绑定到MainViewModel。
我正在使用以下代码:
这里Converter 的第一个值绑定到ChildViewModel 中的属性C 并且它可以工作。我试图将第二个值绑定到 MainWindow (MainViewModel) 的 DataContext 的属性,但没有成功。
<MultiBinding Converter="{StaticResource UnitConverter}">
<Binding Path="C"/>
<Binding RelativeSource="{RelativeSource FindAncestor,
AncestorType={x:Type Window}, AncestorLevel=2}"
Path="DataContext.CurrentTargetUnit"/>
</MultiBinding>
--- Main Window -------------------------------------------
- ----- User Control 1 ------------------------------- -
- - ---- User Control 2-------------------------- - -
- - - ------ Child Window 1 ---------------- - - -
- - - - ----- Child Window 2 ---------- - - - -
- - - - - - - - - -
- - - - - [Bind to MainViewModel] - - - - -
- - - - - - - - - -
- - - - ------------------------------- - - - -
- - - -------------------------------------- - - -
- - --------------------------------------------- - -
- ---------------------------------------------------- -
-----------------------------------------------------------
更新:
我最好的猜测是,一个只能绑定到父窗口并且不能更高。也许最上面的窗口不在可视化树中?
【问题讨论】:
标签: c# wpf xaml data-binding multibinding