views:

107

answers:

1

OK, I have defined a style for Navigation Window. I have successfully styled Navigation buttons and even added page breadcrumbs to the Navigation menu. What I want is to add Page title next to the breadcrumbs:

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>

The binding doesn't work for the last TextBlock. (However it works just fine if not used within a style, but in regular XAML page code-behind) I have no idea why. Help? How to make it display the current page title? Thanks.

A: 

The problem is that within the ControlTemplate, there is no ancestor of type Page. The control you apply the template to may have an ancestor of type Page, but the ControlTemplate itself doesn't know about that. It only knows about ancestors in its own logical tree.

To help mitigate this issue, the WPF designers added the TemplateBinding markup extension, which allows you to apply the value of a property on the templated control to a property in the control template.

So, on the NavigationWindow, you should create a property that exposes the Title of the Page. Then you can use the following mark-up to bind to it:

Text="{TemplateBinding TitleProperty}"
Charlie
Let me be more specific: I have set the setter <Setter Property="Title" Value="{Binding RelativeSource={RelativeSource Self}} />This exposes the title of the Navigation Window, which is the same as Page.WindowTitle. However, what I need is the Page.Title property. Can you help?
Boris