绑定到WPF中的祖先

Ted*_*ord 27 c# data-binding wpf

我在一个程序集中有一个窗口,它有一个TextBlock控件,我想绑定到一个类的属性的值,该类是该窗口父窗口的DataContext的属性.用作DataContext的类仅在第二个程序集中定义.我的问题是我需要在绑定语句中将Type指定为Type.我可以只使用两个程序集之间通用的DataContext属性的类型,还是需要使用DataContext的类型?

以下是我认为它应该如何工作的原型,但因为它不是我对某事感到困惑:)

装配#1
窗口

<TextBlock 
    Text="{Binding RelativeSource={RelativeSource 
        AncestorType={x:Type client:Client}}, Path=Name }"/>
Run Code Online (Sandbox Code Playgroud)

程序集#2
应用程序外壳

class Shell 
{
     public Client Client { get { return client; } set { client = value; } }
     OnStartup()
     {
          NavigationWindow window = new NavigationWindow();
          window.DataContext = this;
          window.Navigate(GetHomeView());
     }
}
Run Code Online (Sandbox Code Playgroud)

dec*_*one 58

以下应该有效:

<TextBlock Text="{Binding RelativeSource={RelativeSource Mode=FindAncestor,
                                                         AncestorType={x:Type Window}},
                                                         Path=DataContext.Client.Name}" />
Run Code Online (Sandbox Code Playgroud)