将参数传递给模板 [英] Passing parameters to a template
本文介绍了将参数传递给模板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
假设我已经定义了一个按钮带有圆角。
Say I have defined a button with rounded corners.
<Style x:Key="RoundButton" TargetType="Button">
<!-- bla bla -->
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Button">
<Border CornerRadius="0,5,5,0" />
<!-- bla bla -->
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
我有可能,这个按钮的用户可以指定CornerRadius?我可以使用TemplateBinding?不过,我应该在哪里绑定? (要标记?)
I it possible that the user of this button can specify the CornerRadius? Can I use a TemplateBinding? But where should I bind to? (to Tag?)
推荐答案
为了使用 TemplateBinding
,也必须在模板化控件的属性(按钮
,在这种情况下)。 按钮
不具有 CornerRadius
或等价的属性,所以你的选择是:
In order to use a TemplateBinding
, there must be a property on the templated control (Button
, in this case). Button
does not have a CornerRadius
or equivalent property, so your options are:
-
C的值
- 硬$ C $模板中
- 劫持另一个属性(如
标签
)来存储这些信息。这是更快,但缺乏类型安全,是难以维持,而prevents其他财产的用途。 - 在子类
按钮
并添加你需要的属性格式,然后提供该子类的模板。这需要一点时间,但产生对消费者的控制更加美好的体验。
- hard code the value in the template
- Hijack another property (such as
Tag
) to store this information. This is quicker, but lacks type safety, is harder to maintain, and prevents other uses of that property. - Subclass
Button
and add the propery you need, then provide a template for that subclass. This takes a little longer but yields a much nicer experience for consumers of your control.
这篇关于将参数传递给模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文