如何覆盖 UserControl 的 ControlTemplate
当我尝试覆盖用户控件时,如何知道它的默认模板元素? 例如,有人像这样重写了 TabControl 的模板。
<TabControl>
<TabControl.Template>
<ControlTemplate TargetType="TabControl">
<StackPanel>
<ScrollViewer HorizontalScrollBarVisibility="Visible" VerticalScrollBarVisibility="Disabled">
<TabPanel x:Name="HeaderPanel"
Panel.ZIndex ="1"
KeyboardNavigation.TabIndex="1"
Grid.Column="0"
Grid.Row="0"
Margin="2,2,2,0"
IsItemsHost="true"/>
</ScrollViewer>
<ContentPresenter x:Name="PART_SelectedContentHost"
SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"
Margin="{TemplateBinding Padding}"
ContentSource="SelectedContent"/>
</StackPanel>
</ControlTemplate>
</TabControl.Template>
<TabItem Header="TabItem1">TabItem1 Content</TabItem>
<TabItem Header="TabItem2">TabItem2 Content</TabItem>
</TabControl>
他怎么知道 TabControl 的模板中有
和
呢?
How can I know the default template element of a usercontrol when I trying to override it?
For example somebody have overrided the TabControl's template like this.
<TabControl>
<TabControl.Template>
<ControlTemplate TargetType="TabControl">
<StackPanel>
<ScrollViewer HorizontalScrollBarVisibility="Visible" VerticalScrollBarVisibility="Disabled">
<TabPanel x:Name="HeaderPanel"
Panel.ZIndex ="1"
KeyboardNavigation.TabIndex="1"
Grid.Column="0"
Grid.Row="0"
Margin="2,2,2,0"
IsItemsHost="true"/>
</ScrollViewer>
<ContentPresenter x:Name="PART_SelectedContentHost"
SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"
Margin="{TemplateBinding Padding}"
ContentSource="SelectedContent"/>
</StackPanel>
</ControlTemplate>
</TabControl.Template>
<TabItem Header="TabItem1">TabItem1 Content</TabItem>
<TabItem Header="TabItem2">TabItem2 Content</TabItem>
</TabControl>
How does he know there is <StackPanel>
and <ContentPresenter>
in the TabControl's template?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
当您重写任何控件的 ControlTemplate 时,您就是在定义它的外观。
只是您正在使用的布局控件,它可以是网格或任何其他布局控件。然而,这是它需要存在的东西。如果查看 WPF 控件层次结构,您可以在 Control、FrameworkElement 等之后看到基本级别的多种类型的控件:
其中每一项都有特定的呈现选项和部分。在您的情况下,
是一个
,它是
的特殊类型。该选择器有一个 Content 和一个 TabPanel,即
和
(它告诉 WPF 在何处呈现内容)。获取此知识的最佳方法是查看每个控件的默认 WPF 模板,例如 WPF4 的 TabControl 默认模板是 此处
When you are overriding a ControlTemplate for any control, you are defining how it will look. The
<StackPanel>
is just the layout control you are using, it could be a grid or any other layout control.However the is something it needs to be there. If you look at the WPF control hierarchy, you can see several types of controls at the base levels, after Control, FrameworkElement, etc:
<ContentControl>
<HeaderedContentControl>
<ItemsControl>
<HeaderedItemsControl>
Each one of these have specific rendering options and parts. In your case a
<TabControl>
is an<Selector>
which is a special type of an<ItemsControl>
. This Selector has a Content and a TabPanel, thus the<TabPanel>
and the<ContentPresenter>
(which tells WPF where to render the Content).The best way to aquire this knowledge is by looking at the default WPF templates for each control, for example the TabControl default template for WPF4 is here
请参阅 MSDN 了解默认模板和样式。
See MSDN for default templates and styles.
TabControl
类有一个[TemplatePart]
属性,指示模板的强制部分:在这种情况下,模板必须包含一个名为
PART_SelectedContentHost
的ContentPresenter
。其他一切都是可选的,您可以在模板中添加任何您喜欢的内容(当然只要它有意义)。The
TabControl
class has a[TemplatePart]
attribute that indicates mandatory parts of the template:In this case the template must contain a
ContentPresenter
namedPART_SelectedContentHost
. Everything else is optional, you can put anything you like in the template (as long as it makes sense of course).