Richard.Davenport
Richard.Davenport

Reputation: 1501

HeaderedContentControl header template not being used

I'm fairly new to WPF and am working with some legacy code, not sure how to use the HeaderedContentControl Header. I'd like to put in a StackPanel and customize the look of a header, just not sure how to do that.

Could someone give me some guidance on what to do next?

I have this xaml and the HeaderTemplate is never used.

<UserControl x:Class="PEC.Admin.WindowsControls.Program.Views.ProgramProductEnrichmentColorsView"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         xmlns:commonControls="clr-namespace:ManagerConsole.Common.Controls;assembly=ManagerConsole.Common.Controls"
         xmlns:program="clr-namespace:PEC.Admin.ViewModel.Program;assembly=PEC.Admin.ViewModel.Program"
         mc:Ignorable="d" 
         d:DesignWidth="300" 
         d:DataContext="{d:DesignInstance program:ProgramProductEnrichmentColorsViewModel}">
    <commonControls:ExpanderPanel IsExpanded="{Binding Path=IsExpanded,Mode=TwoWay}">
        <HeaderedContentControl.HeaderTemplate> <!-- this never gets used... -->
            <DataTemplate>
                <StackPanel>
                    <Label Content="{Binding Path=Header}"></Label>
                </StackPanel>
            </DataTemplate>
        </HeaderedContentControl.HeaderTemplate>
        <StackPanel HorizontalAlignment="Stretch"
                    VerticalAlignment="Top"
                    Width="Auto"
                    Margin="3"
                    Background="White">
            <TextBlock Text="Source Type:"
                       Margin="0,5,0,0" />
            <TextBox IsReadOnly="True"
                     IsTabStop="False"
                     Background="LightGray"
                     BorderThickness="0"
                     Text="{Binding Path=SourceTypeName, Mode=OneTime}" />
        </StackPanel>
    </commonControls:ExpanderPanel>
</UserControl>

Upvotes: 1

Views: 926

Answers (1)

ASh
ASh

Reputation: 35730

HeaderTemplate is applied. To verify it - set a background to the Label in HeaderTemplate.

HeaderTemplate doesn't display anything though, because binding is incorrect. Template is applied to the data set in Header property, which currently has null value.

So change code as in the example below (I tried with Expander, hopefully it will work for custom commonControls:ExpanderPanel):

<Expander IsExpanded="{Binding Path=IsExpanded, Mode=TwoWay}" 
          Header="{Binding ComplexObject}">
  <HeaderedContentControl.HeaderTemplate>
    <DataTemplate>
        <StackPanel>
            <Label Background="Green" Content="{Binding PropertyOfTheObject}"/>
        </StackPanel>
    </DataTemplate>
  </HeaderedContentControl.HeaderTemplate>
</Expander>

Header is a dependency property and can be set via Binding. Header becomes a source for bindings in a HeaderTemplate. Or it can be some constant (Header="Click to expand"), resource (Header="{StaticResource ExpandTitle}") or complex content, e.g.:

<Expander.Header>
   <TextBlock Text="Click to expand"/>
</Expander.Header>

Upvotes: 2

Related Questions