2010-08-04 90 views
18

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

下面是如何,我认为它应该工作的原型,但因为它不是我感到困惑的东西:)

大会#1
窗口

<TextBlock 
    Text="{Binding RelativeSource={RelativeSource 
     AncestorType={x:Type client:Client}}, Path=Name }"/> 

大会#2
应用壳牌

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

回答

45

以下应该工作:

<TextBlock Text="{Binding RelativeSource={RelativeSource Mode=FindAncestor, 
                 AncestorType={x:Type Window}}, 
                 Path=DataContext.Client.Name}" /> 
+0

真棒,做了诡计! – Tedford 2010-08-04 14:50:50