【发布时间】:2012-11-02 11:22:33
【问题描述】:
我想将一个属性绑定到在其 DataContext 中具有 ViewModel 的父容器视图。
当父级是 ConcreteClassView 的直接实例时,此代码运行良好:
Property="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ty:ConcreteClassView}}, Path=DataContext.Name}"
但是,当尝试通过基类或接口定位父级时,找不到父级。 示例:
PropertyB="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ty:BaseClassView}}, Path=DataContext.Name}"
PropertyB="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ty:INamedElementView}}, Path=DataContext.Name}"
鉴于:
class ConcreteClassView : BaseClassView, INamedElementView { }
好的,假设 FindAncestor、AncestorType 需要具体类型才能工作。
但是有任何解决方法可以仅基于基类或实现给定接口来定位祖先吗?
谢谢。
【问题讨论】:
-
奇怪,
AncestorType也必须与基类一起使用。 -
你检查过命名空间是否正确吗?也许
INamedElementView在其他命名空间中? -
感谢@Vlad 所有这些都定义在同一个命名空间中。
ty别名引用它。 -
我也有同样的问题。
-
@pjmolina:你解决了这个问题吗?这是我在回答中描述的案例之一吗?
标签: c# wpf binding interface relativesource