【发布时间】:2016-10-17 19:34:15
【问题描述】:
可能,我理解的东西根本上是错误的,但根据我的阅读,ControlTemplate-Property 允许我完全定义控件的外观,而无需更改其逻辑(如事件等)。
为了进行一些测试,我创建了一个空的 UWP-Project,并创建了一个带有拉伸功能的 Button:
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<Button HorizontalAlignment="Stretch"
Background="Red">
</Button>
</Grid>
正如预期的那样,按钮延伸到整个窗口。 现在,我用基本完全相同的按钮覆盖模板,但作为模板:
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<Button>
<Button.Template>
<ControlTemplate>
<Button HorizontalAlignment="Stretch"
Background="Red" />
</ControlTemplate>
</Button.Template>
</Button>
</Grid>
在这种情况下,HorizontalAlignment 会被忽略。 由于我基本上用默认按钮模板(一个按钮)覆盖了按钮的外观,因此我预计会有相同的行为。我可以通过在 Button 本身上定义 HorizontalAlignment 然后使用 TemplateBinding 来修复它,但我不明白为什么它没有像预期的那样被覆盖。
【问题讨论】: