模板绑定与绑定是什么? [英] What is the template binding vs binding?

查看:93
本文介绍了模板绑定与绑定是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我听不懂BorderThickness="{TemplateBinding BorderThickness}. 这里的代码:

I could not understand BorderThickness="{TemplateBinding BorderThickness}. Here the code:

<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.

推荐答案

TemplateBinding用于绑定到模板定义中的元素属性.在您的示例中,您可能已经写过

TemplateBinding is used for binding to the element properties within the template definition. In your example, you could have written

 <Border Padding="{Binding Padding}" ...>

的意思是将边框的padding属性绑定到...的padding属性.什么?您想说的是,此模板用于的控件的padding属性."您不能给它起一个名字,因为此时您不知道控件的x:Name(即使您知道,它也不会起作用,因为它在另一个名称范围中).但是,您可以通过定义相对来源来做到这一点

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

<Border Padding="{Binding Padding, RelativeSource={RelativeSource TemplatedParent}" ...>

或使用TemplateBinding,它是上述的快捷方式(*)

or use TemplateBinding which is a shortcut(*) for above

<Border Padding="{TemplateBinding Padding}" ...>


(*)除了在这些模板化场景中不太冗长之外,与常规绑定相比,TemplateBinding还有一些区别:


(*) In addition to being less verbose in these templating scenarios, TemplateBinding has a couple of differences compared to a regular binding:

  • 在编译时进行评估. (例如,如果不存在Padding属性,则将出现编译错误.但是,如果要对TemplatedParent使用绑定,则只会在运行时看到错误.)
  • 它始终是单向绑定.
  • 它要求源属性和目标属性都是依赖属性.
  • 它的功能要少得多(没有StringFormat,Delay,IsAsync等.)请参见 TemplateBindingExtention ).
  • 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 Binding vs TemplateBindingExtention).

这篇关于模板绑定与绑定是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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