Loetn
Loetn

Reputation: 4040

Nested ContentControls with template

I have a custom WindowStyle, the XAML looks like this:

    <Style TargetType="{x:Type Window}"
       x:Key="WindowStyle">
    /** Some setters **/
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="ContentControl">
                <AdornerDecorator>
                    <Grid Background="#88000000"
                          x:Name="WindowBackgroundGrid">
                        <Border x:Name="WindowContentBorder"
                                Background="{DynamicResource WindowBackground}"MaxHeight="{Binding Source={x:Static SystemParameters.FullPrimaryScreenHeight}}"
                                MaxWidth="{Binding Source={x:Static SystemParameters.FullPrimaryScreenWidth}}"
                                Margin="20">
                            <Grid>
                                <Grid.RowDefinitions>
                                    <RowDefinition Height="Auto" />
                                    <RowDefinition Height="*" />
                                </Grid.RowDefinitions>

                                <!-- Header -->
                                <Border BorderBrush="{DynamicResource BorderBrushColor}"
                                        Background="{DynamicResource PaneHeader_Background}"
                                        Grid.Row="0">
                                    <TextBlock Text="Title"Foreground="{DynamicResource DefaultForeground}"
                                               FontSize="16"
                                               FontWeight="Bold"
                                               Margin="5,5,2,5" />
                                </Border>

                                <!-- Content -->
                                <ScrollViewer Grid.Row="1"
                                              Margin="5">
                                    <ContentPresenter Content="{TemplateBinding Content}" />
                                </ScrollViewer>
                            </Grid>
                        </Border>
                    </Grid>
                </AdornerDecorator>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

Now I want the inner Grid in a seperate Style so that I can use it elsewhere.

 <Style x:Key="WindowContentStyle"
       TargetType="{x:Type ContentPresenter}">
    <Setter Property="ContentTemplate">
        <Setter.Value>
            <DataTemplate>
                <Grid>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="Auto" />
                        <RowDefinition Height="*" />
                    </Grid.RowDefinitions>

                    <!-- Header -->
                        /** Border control **/
                    <!-- Content -->
                    <ScrollViewer Grid.Row="1" 
                                  Margin="5">
                        <ContentPresenter Content="{Binding Content, RelativeSource={RelativeSource TemplatedParent}}" />
                    </ScrollViewer>
                </Grid>
            </DataTemplate>
        </Setter.Value>
    </Setter>
</Style>

And I use a ContenPresenter in my WindowStyle to present it:

<ContentPresenter>
    <ContentPresenter.Style>
        <Style TargetType="{x:Type ContentPresenter}"
               BasedOn="{StaticResource WindowContentStyle}" />
    </ContentPresenter.Style>
</ContentPresenter>

Problem

The edit above didn't give me any errors, but it doesn't present my WindowContentStyle. When I set the Content property of the Window control and load the style

this.window.Content = view;
this.window.Style = (Style)Application.Current.TryFindResource("WindowStyle");

the content is shown in the ContentPresenter in the WindowStyle and not in WindowContentStyle. Because of this, the Template is not used and I don't have a header with a title.

How can I make my outer ContentPresenter pass on the Content to my inner ContentPresenter (the one in WindowContentStyle)?

Thanks in advance!

Greetings Loetn

Upvotes: 0

Views: 1208

Answers (1)

Sheridan
Sheridan

Reputation: 69987

You should use a ContentControl to display your content, not a ContentPresenter. From the ContentPresenter Class page on MSDN:

You typically use the ContentPresenter in the ControlTemplate of a ContentControl to specify where the content is to be added.

From the ContentControl Class page on MSDN:

A ContentControl has a limited default style. If you want to enhance the appearance of the control, you can create a new DataTemplate.

Upvotes: 1

Related Questions