Scott Maher
Scott Maher

Reputation: 35

How To Extend A DataTemplate to Include an Additional Control

So I've got a style, which does some fancy things to a wpf TabControl, among which it sets a HeaderTemplate that allows users to rename the tab. I've got a style which is based on this template that colors the text based on some parameter, and I would like to add a polygon to the header of each tab depending on some conditions.

So the base style looks like:

<Style TargetType="TabItem" x:Key="EditableTabItemStyle" 
       BasedOn="{StaticResource BaseTabItemStyle}">
    <Setter Property="HeaderTemplate">
        <Setter.Value>
            <DataTemplate>
                <controls:EditableTabContent Template="{StaticResource EditableTabItemTemplate}"
                                             Content="{Binding Path=., Mode=TwoWay}"/>
            </DataTemplate>
        </Setter.Value>
    </Setter>
</Style>

and my derived looks like:

<Style x:Key="DerivedTabItem"
     BasedOn="{StaticResource EditableTabItemStyle}"
     TargetType="TabItem">
<Setter Property="Foreground">
  <Setter.Value>
    <MultiBinding Converter="{StaticResource StateToColorConverter}">
      <Binding Path="States.State1" />
      <Binding Path="States.State2" />
    </MultiBinding>
  </Setter.Value>
</Setter>

I'm looking to add a polygon to the derived style, preferably without changing the base style at all, so my initial thought was to add a header template:

<Setter Property="HeaderTemplate">
  <Setter.Value>
      <!-- Old header template -->
      <!-- Polygon -->
  </Setter.Value>
</Setter>

But that overrides the editable styling. I'm not sure if what I want is even possible, so I'll take that for an answer if need be, but if it can be done, how can I do it?

Upvotes: 1

Views: 596

Answers (1)

Eli Arbel
Eli Arbel

Reputation: 22739

You can't amend an existing template. You can either:

  • Copy it and add anything you like.
  • Modify the original template to include another control that's only displayed under certain conditions (e.g. using a DataTrigger or a binding to its Visibility property).
    • This control could be something very flexible, such as a ContentPresenter that could display all sorts of content by binding it to some placeholder property.

In this case, where the template is so simple, I'd go with the former.

Upvotes: 1

Related Questions