2010-08-04 87 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