2009-08-24 66 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