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が機能するために具象型が必要であると仮定しましょう。
しかし、基本クラスに基づいて、または特定のインターフェイスを実装するだけで先祖を見つけるための回避策はありますか?
なるほど。