wpf 什么是模板绑定与绑定?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10597492/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is the template binding vs binding?
提问by prjndhi
I could not understand BorderThickness="{TemplateBinding BorderThickness}
.
Here the code:
我不明白BorderThickness="{TemplateBinding BorderThickness}
。这里的代码:
<ControlTemplate TargetType="{x:Type wpftoolkit:DataGridCell}">
<Border Padding="{TemplateBinding Padding}"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}"
Background="{TemplateBinding Background}"
SnapsToDevicePixels="True">
<ContentPresenter SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
</Border>
</ControlTemplate>
Also please explain other types of binding.
还请解释其他类型的绑定。
回答by Eren Ers?nmez
TemplateBinding is used for binding to the element properties within the template definition. In your example, you could have written
TemplateBinding 用于绑定到模板定义中的元素属性。在你的例子中,你可以写
<Border Padding="{Binding Padding}" ...>
meaning to bind the border's padding property to the padding property of... what?You'd like to say, "padding property of the control that this template is being used for." You can't give it a name because you don't know the x:Name of the control at this time (even if you did, it wouldn't work because its in a different namescope). However, you can do this by defining a relative source
意思是将边框的 padding 属性绑定到……什么的 padding 属性?您想说,“此模板所用于的控件的填充属性。” 您不能给它命名,因为此时您不知道控件的 x:Name(即使您知道,它也不会工作,因为它在不同的名称范围内)。但是,您可以通过定义相对源来做到这一点
<Border Padding="{Binding Padding, RelativeSource={RelativeSource TemplatedParent}" ...>
or use TemplateBinding which is a shortcut(*) for above
或使用 TemplateBinding 这是上面的快捷方式(*)
<Border Padding="{TemplateBinding Padding}" ...>
(*) In addition to being less verbose in these templating scenarios, TemplateBinding has a couple of differences compared to a regular binding:
(*) 除了在这些模板场景中不那么冗长之外,TemplateBinding 与常规绑定相比还有一些不同:
- It is evaluated at compile-time. (if, for example, Padding property didn't exist, you would get a compile error. But if you were to use a binding with TemplatedParent, you would only see the error at runtime.)
- It is always a one-way binding.
- It requires that both the source and target properties are dependency properties.
- It has much less functionality (no StringFormat, Delay, IsAsync, etc.. see the properties of Bindingvs TemplateBindingExtention).
- 它在编译时进行评估。(例如,如果 Padding 属性不存在,则会出现编译错误。但如果要使用 TemplatedParent 绑定,则只会在运行时看到错误。)
- 它始终是一种单向绑定。
- 它要求源属性和目标属性都是依赖属性。
- 它的功能要少得多(没有 StringFormat、Delay、IsAsync 等。请参阅Binding与TemplateBindingExtention的属性)。
回答by Bad
A picture is worth a thousand words. In this case it is 7 minutes video: https://www.youtube.com/watch?v=z-0TZR-7xLI
一张图片胜过千言万语。在这种情况下,它是 7 分钟的视频:https: //www.youtube.com/watch?v=z-0TZR-7xLI
EDIT:Example:
编辑:示例:
- A
Button
has a defaultControlTemplate
property andHeight
property - You override
ControlTemplate
property of aButton
by writing your own (for example you want to make anEllipse
-looking button instead ofRectangle
-looking) - After you made an
Ellipse
in your newControlTemplate
, you want theEllipse
to be the same size as original Button'sHeight
property - So you use
TemplateBinding
in order to referenceButton
'sHeight
without naming it
回答by dowhilefor
Eren Ers?nmenz already explained it quite well, but i would like to give it another perspective to better understand the concept.
Eren Ers?nmenz 已经很好地解释了它,但我想给它另一个角度来更好地理解这个概念。
In WPF every control is more or less detached from its presentation. You can always change the template of controls and make it look completely different. A button works as expected with a ControlTemplate
only consisting of a Rectangle
for example. Now sometimes it is necessary for the ControlTemplate
to actually use the properties of the logic part of a control. And thats what TemplateBinding
is for it just tells the ControlTemplate
"Use this property of the control we are giving the visual presentation".
A good example is the Background
property on every control, it has no meaning on its own, it gets its meaning by TemplateBinding
it to child control in the ControlTemplate
.
在 WPF 中,每个控件或多或少都与其表示分离。您可以随时更改控件模板并使其看起来完全不同。按钮按预期工作,例如ControlTemplate
仅由 a 组成Rectangle
。现在有时需要ControlTemplate
实际使用控件逻辑部分的属性。这就是TemplateBinding
它只是告诉ControlTemplate
“使用我们提供视觉呈现的控件的这个属性”。一个很好的例子是Background
每个控件上的属性,它本身没有意义,它通过TemplateBinding
它在ControlTemplate
.
Binding on its own is very good described in the MSDN. Thisis a very nice cheat sheet which in fact hangs on my wall right next to me. It gives a good overview of all the different bindings available.
MSDN 中对自身的绑定进行了很好的描述。这是一个非常好的备忘单,它实际上挂在我旁边的墙上。它很好地概述了所有可用的不同绑定。
回答by Zabavsky
From TemplateBinding Markup Extension, TemplateBinding
links the value of a property in a control template to the value of some other exposed property on the templated control. Other words, it is for binding values in a template.
从TemplateBinding Markup Extension,TemplateBinding
将控件模板中的属性值链接到模板化控件上的某些其他公开属性的值。换句话说,它用于绑定模板中的值。
Bindingconnects a property of binding targets and data sources.
绑定连接绑定目标和数据源的属性。