通过更改控件模板和添加子控件来修改控件之间的区别是什么

时间:2021-12-13 16:03:04

I am new to WPF and I have a doubt in modifying a control. I came to know that one can modify a control by changing its Control Template. i.e.,

我是WPF的新手,我对修改控件有疑问。我开始知道可以通过更改控件模板来修改控件。即,

<Button>
  <Button.Template>
    <ControlTemplate>
      ...
    </ControlTemplate>
  </Button.Template>
</Button>

Also we can edit a control utilizing the Content Model. i.e.,

我们也可以使用内容模型编辑控件。即,

<Button>
  <Grid>
    <CheckBox>
    </CheckBox>
    <RadioButton>
    </RadioButton>
    ...
  </Grid>
</Button>

What is the difference between these two techniques. Which one is the correct way to customize a control in WPF. Please help me in understanding this better.

这两种技术有什么区别。哪一种是在WPF中自定义控件的正确方法。请帮助我更好地理解这一点。

3 个解决方案

#1


The primary difference between these two things, is that the ControlTemplate defines the look of the control. It is not actually placing content inside of it. At some location inside the content control, there should be some form of ContentPresenter. The built in controls are capable of this because they are what is known as 'lookless controls', and any custom controls created should also be lookless. When a control is not templated in a lookless manner but instead has a static layout, then the confusion you have run into can occur.

这两件事之间的主要区别在于ControlTemplate定义了控件的外观。实际上并没有将内容放在其中。在内容控件内的某个位置,应该有某种形式的ContentPresenter。内置控件具有这种能力,因为它们被称为“无视控件”,并且创建的任何自定义控件也应该是无形的。如果控件没有以无形的方式进行模板化,而是具有静态布局,那么您可能会遇到混乱。

As for the correct way to do things, it depends on what you are trying to achieve. If you are attempting to change the control, such as the look and feel or the behavior, then using a ControlTemplate, (or DataTemplate, depending on what you are templating), is definitely the way to go. A good example of this is the CheckBox, belive it or not, the CheckBox is actually a ToggleButton (more or less), that through templating displays the togleablity in a bullet. Here's another very good example of how you can use Templates to do some very nifty things.

至于正确的做事方式,这取决于你想要达到的目标。如果您正在尝试更改控件(例如外观或行为),那么使用ControlTemplate(或DataTemplate,取决于您的模板),绝对是可行的方法。一个很好的例子是CheckBox,信不信由你,CheckBox实际上是一个ToggleButton(或多或少),通过模板显示子弹中的togleablity。这是另一个很好的例子,说明如何使用模板做一些非常漂亮的事情。

ControlTemplates should also be applied through Styles, instead of directly set on an element.

ControlTemplates也应该通过样式应用,而不是直接在元素上设置。

If you aren't actually aiming to modify the behavior or look of the control then using the content model is the correct approach.

如果您实际上并不打算修改控件的行为或外观,那么使用内容模型是正确的方法。

WPF does this better then Silverlight, though I don't know if there are improvements in SL3.

WPF比Silverlight更好,但我不知道SL3是否有改进。

#2


My rule of thumb is that if it's possible to get what I want without changing the control template then you shouldn't change the control template.

我的经验法则是,如果可以在不更改控件模板的情况下获得我想要的内容,则不应更改控件模板。

control templates are a way to change the look&feel of a control, for example making a round button of changing the check mark graphics of a checkbox.

控件模板是一种更改控件外观的方法,例如,创建一个更改复选框的复选标记图形的圆形按钮。

adding controls inside a control is simpler, for example place an image inside a button.

在控件内添加控件更简单,例如将图像放在按钮内。

Also remember, there is a lot of functionality in the control template, including padding, content alignment, disabled look and more, if you change the control template you will have to add support for all of those features.

还要记住,控件模板中有很多功能,包括填充,内容对齐,禁用外观等,如果更改控件模板,则必须添加对所有这些功能的支持。

#3


Template can be placed into a resource and re-used for another button.

模板可以放入资源并重新用于另一个按钮。

Changing content directly is not re-usable unless you make a UserControl out of it.

直接更改内容不可重复使用,除非您使用UserControl。

Which one you use depends on a concrete task and your personal preferences.

您使用哪一个取决于具体任务和您的个人偏好。

#1


The primary difference between these two things, is that the ControlTemplate defines the look of the control. It is not actually placing content inside of it. At some location inside the content control, there should be some form of ContentPresenter. The built in controls are capable of this because they are what is known as 'lookless controls', and any custom controls created should also be lookless. When a control is not templated in a lookless manner but instead has a static layout, then the confusion you have run into can occur.

这两件事之间的主要区别在于ControlTemplate定义了控件的外观。实际上并没有将内容放在其中。在内容控件内的某个位置,应该有某种形式的ContentPresenter。内置控件具有这种能力,因为它们被称为“无视控件”,并且创建的任何自定义控件也应该是无形的。如果控件没有以无形的方式进行模板化,而是具有静态布局,那么您可能会遇到混乱。

As for the correct way to do things, it depends on what you are trying to achieve. If you are attempting to change the control, such as the look and feel or the behavior, then using a ControlTemplate, (or DataTemplate, depending on what you are templating), is definitely the way to go. A good example of this is the CheckBox, belive it or not, the CheckBox is actually a ToggleButton (more or less), that through templating displays the togleablity in a bullet. Here's another very good example of how you can use Templates to do some very nifty things.

至于正确的做事方式,这取决于你想要达到的目标。如果您正在尝试更改控件(例如外观或行为),那么使用ControlTemplate(或DataTemplate,取决于您的模板),绝对是可行的方法。一个很好的例子是CheckBox,信不信由你,CheckBox实际上是一个ToggleButton(或多或少),通过模板显示子弹中的togleablity。这是另一个很好的例子,说明如何使用模板做一些非常漂亮的事情。

ControlTemplates should also be applied through Styles, instead of directly set on an element.

ControlTemplates也应该通过样式应用,而不是直接在元素上设置。

If you aren't actually aiming to modify the behavior or look of the control then using the content model is the correct approach.

如果您实际上并不打算修改控件的行为或外观,那么使用内容模型是正确的方法。

WPF does this better then Silverlight, though I don't know if there are improvements in SL3.

WPF比Silverlight更好,但我不知道SL3是否有改进。

#2


My rule of thumb is that if it's possible to get what I want without changing the control template then you shouldn't change the control template.

我的经验法则是,如果可以在不更改控件模板的情况下获得我想要的内容,则不应更改控件模板。

control templates are a way to change the look&feel of a control, for example making a round button of changing the check mark graphics of a checkbox.

控件模板是一种更改控件外观的方法,例如,创建一个更改复选框的复选标记图形的圆形按钮。

adding controls inside a control is simpler, for example place an image inside a button.

在控件内添加控件更简单,例如将图像放在按钮内。

Also remember, there is a lot of functionality in the control template, including padding, content alignment, disabled look and more, if you change the control template you will have to add support for all of those features.

还要记住,控件模板中有很多功能,包括填充,内容对齐,禁用外观等,如果更改控件模板,则必须添加对所有这些功能的支持。

#3


Template can be placed into a resource and re-used for another button.

模板可以放入资源并重新用于另一个按钮。

Changing content directly is not re-usable unless you make a UserControl out of it.

直接更改内容不可重复使用,除非您使用UserControl。

Which one you use depends on a concrete task and your personal preferences.

您使用哪一个取决于具体任务和您的个人偏好。