样式和控件模板之间的差异 [英] Difference between Style and ControlTemplate

查看:112
本文介绍了样式和控件模板之间的差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你能告诉我什么是样式和控件模板之间的主要区别是什么? 当或为什么要使用一个或其他?

Could you tell me what is the main differences between Style and ControlTemplate ? When or why to use one or the other ?

在我眼里,他们是完全很同一。由于我初学我认为我是错的,因此我的问题。

To my eyes, they are exactly the very same. As I am beginner I think that I am wrong, thus my question.

推荐答案

在您设置一个控件的属性,风格

In a style you set properties of a control.

在模板定义的控制的用户界面,并使用在该样式中设置的属性(和它们的值)。

In a template you define the UI of the control and use the properties (and their values) that are set in the style.

模板中设置的值,只能通过更换模板,而在一个样式的值可以通过使用控制时设置值替换被替换。这就是为什么是更好利用TemplateBinding代替编码值使用控制的属性。

Values set in the template can only be replaced by replacing the template while values in a style can be replaced by setting the value when using the control. That is why is better to use the properties of the control by using TemplateBinding instead of coding values.

另一件事是,控制可以拿起一个默认的样式,而无需特定的样式被分配给他们。你不能做到这一点的一个模板。

Another thing is that control can pick up a default style without having a specific style being assigned to them. You can't do that with a template.

要复杂的是:你可以在样式模板设置属性:

To complicate matters: you can set the Template property in the style:

<Style TargetType="Button">
    <Setter Property="Background" Value="Red"/>
    <Setter Property="Template">
        <Setter.Value>
             <ControlTemplate TargetType="Button">
                 <Grid>
                     <Rectangle Fill="{TemplateBinding Background"/>
                     <ContentPresenter/>
                 </Grid>
             </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

这篇关于样式和控件模板之间的差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆