2009-08-24 68 views
1

好的,我已經定義了導航窗口的樣式。我已成功設置了導航按鈕樣式,甚至將頁面麪包屑添加到導航菜單。我想要的是添加旁邊的麪包屑頁面標題:將頁面標題綁定到樣式中的文本塊

Style x:Key="{x:Type NavigationWindow}" TargetType="NavigationWindow"> 
    <Setter Property="SnapsToDevicePixels" Value="true"/> 
     <Setter Property="Template"> 
     <Setter.Value> 
      <ControlTemplate TargetType="NavigationWindow"> 
       <DockPanel Background="{StaticResource WindowBackgroundBrush}"> 
       ... 
       <Grid> 
      <Grid.ColumnDefinitions> 
         <ColumnDefinition Width="Auto"/> 
         <ColumnDefinition Width="Auto"/> 
         <ColumnDefinition Width="16"/> 
         <ColumnDefinition Width="*"/> 
        </Grid.ColumnDefinitions> 
     .... 
     <StackPanel Grid.Column="4" Orientation="Horizontal"> 
         <TextBlock Foreground="Gray" 
            VerticalAlignment="Center" 
            Text="{Binding Path=Title, 
                RelativeSource={RelativeSource FindAncestor, 
                AncestorType={x:Type Page}}}" /> 
        </StackPanel> 
       </Grid> 
      </DockPanel> 
    ... 
     </ControlTemplate> 
    </Setter.Value> 
</Setter> 

綁定不過去TextBlock的工作。 (然而,如果不在樣式中使用,但在常規的XAML頁面代碼隱藏中,它工作得很好),我不知道爲什麼。幫幫我?如何讓它顯示當前頁面標題?謝謝。

回答

0

問題是在ControlTemplate內沒有Page類型的祖先。 將模板應用於的控件可能具有Page類型的祖先,但ControlTemplate本身並不知道這一點。它只知道自己的邏輯樹中的祖先。

爲了幫助緩解此問題,WPF設計人員添加了TemplateBinding標記擴展,它允許您將模板控件上的屬性值應用於控件模板中的屬性。

因此,在NavigationWindow上,您應該創建一個屬性,以顯示PageTitle。然後你可以使用下面的加價綁定到它:

Text="{TemplateBinding TitleProperty}" 
+0

讓我更具體: 我已經設置了二傳手 這暴露了導航窗口的標題,與Page.WindowTitle相同。但是,我需要的是Page.Title屬性。你能幫忙嗎? – Boris 2009-08-25 17:18:48