HeaderTemplate with multiple elements

I am trying to write a HeaderTemplate for an expander. So far, I have noticed that all examples use the {Binding} keyword to retrieve data from the header. However, what happens if there are several controls in the header? How to indicate that these controls should be inserted in a specific place?

 <Window.Resources> <Style x:Key="ExpanderStyle" TargetType="{x:Type Expander}"> <Setter Property="HeaderTemplate"> <Setter.Value> <DataTemplate> <!-- what do I put in here? --> </DataTemplate> </Setter.Value> </Setter> </Style> </Window.Resources> <Expander Style="{StaticResource ExpanderStyle}"> <Expander.Header> <StackPanel Orientation="Horizontal"> <TextBlock>Some Text</TextBlock> <TextBlock Text="{Binding SomeBinding}" /> <Button /> </StackPanel> </Expander.Header> <Image Source="https://www.google.com/logos/2012/steno12-hp.jpg" /> </Expander> 

Should I move the binding to a HeaderTemplate in style and just rewrite all Header in Expander ?

+4
source share
2 answers

You can use ContentPresenter to insert any regular content into your template.

For instance:

 <Style x:Key="ExpanderStyle" TargetType="{x:Type Expander}"> <Setter Property="HeaderTemplate"> <Setter.Value> <DataTemplate> <Border BorderBrush="Blue" BorderThickness="2"> <ContentPresenter /> </Border> </DataTemplate> </Setter.Value> </Setter> </Style> 
+2
source

The Content header property can contain only one object.

If you combine this object into one panel:

 <StackPanel> <TextBlock>Some Text</TextBlock> <TextBlock Text="{Binding SomeBinding}" /> <Button /> </StackPanel> 

then in the template you can use {binding}

0
source

Source: https://habr.com/ru/post/1390452/


All Articles