带有多个项目的 HeaderTemplate [英] HeaderTemplate with multiple items
问题描述
我正在尝试为扩展程序编写一个 HeaderTemplate
.到目前为止,我注意到所有示例都使用 {Binding}
关键字从标题中获取数据.但是,如果 Header 中有多个控件会发生什么?如何指定这些控件应插入到特定位置?
I'm trying to write a HeaderTemplate
for an extender. So far, I've noticed all the examples use the {Binding}
keyword to get the data from the header. However, what happens if there are multiple controls within the Header? How do I specify that those controls should be inserted at a specific location?
<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>
我是否应该将我的绑定移动到样式中的 HeaderTemplate
并只覆盖 Expander
中的 Header
是什么?
Should I be moving my binding into the HeaderTemplate
in the style and just overwriting whatever the Header
in the Expander
is?
推荐答案
您可以使用 ContentPresenter
将任何通常的内容插入到您的模板中
You can use ContentPresenter
to insert whatever the usual content would be into your Template
例如:
<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>
这篇关于带有多个项目的 HeaderTemplate的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!