views:

899

answers:

1

I would like to create a simple control that inherits from HeaderedContentControl, and has some basic dependency properties called Title, Subtitle, Icon. I would like to be able to provide a default header template that databinds these properties. For this example, I have named this class HeaderedView.

I am having trouble in providing a default header template that can bind to the properties defined on the HeaderedView. I am experimenting with markup like the following:

<Style TargetType="{x:Type local:HeaderedView}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type HeaderedContentControl}">
                <StackPanel>
                    <Grid>
                        <ContentPresenter ContentSource="Header"/>
                    </Grid>
                    <Grid>
                        <ContentPresenter ContentSource="Content"/>
                    </Grid>
                </StackPanel>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
    <Setter Property="HeaderTemplate">
        <Setter.Value>
            <DataTemplate>
                <Grid>
                    <TextBlock Text="{TemplateBinding local:HeaderedView.Title}" />
                </Grid>                    
            </DataTemplate>
        </Setter.Value>
    </Setter>
</Style>

Unfortunately, the Title is not being displayed.

The header template must be replaceable (which is why I want to utilize the HeaderedContentControl).

Every time I seem to want to inherit from this control, I seem to struggle with the implementation. Any help would be greatly appreciated!

+2  A: 

In your template, you are using a ContentPresenter to display the Header, but you're not telling the ContentPresenter that it needs to use the HeaderTemplate. You should be able to do this in order to see your custom HeaderTemplate applied:

<ContentPresenter ContentSource="Header" ContentTemplate="{TemplateBinding HeaderTemplate}" />

Also, if you're only planning on changing the HeaderTemplate, then you don't need to override the Template in the first place. The default HeaderedContentControl will apply your HeaderTemplate appropriately.

mjeanes