1. Binding
<targetObject targetProperty="{Binding}" ...> ... </targetObject>
或
<targetObject targetProperty="{Binding [Path=]bindingSourceProperty}" ...> ... </
targetObject>
2. StaticResource
<object propertyName="{StaticResource resourceKey}" ...> ... </ object>
resourceKey 是被引用的资源的键,由资源定义时的 x:Key 或 x:Name 指定
3. TemplateBinding
<templateObject templateProperty="{TemplateBinding [Property=] sourceObjectProperty}"
...> ... </templateObject>
templateObject 是 模 板 对 象 ;templateProperty 是 模 板 对 象 的 属 性 ;Property
是 TemplateBinding 的 属 性, 用 于 指 定 被 绑 定 对 象 的 属 性, 通 常 可 省 略 不 写 ;
sourceObjectProperty 是被绑定对象的属性。
4. RelativeSource
TemplatedParent 模式通常使用于 ControlTemplate 元素定义中
<object property="{Binding RelativeSource={RelativeSource [Mode=]modeValue}}" ...>
... </object>
或
<object property="{Binding RelativeSource={RelativeSource [Mode=]
modeValue},[Path=]bindedProperty}" ...> ... </object>
Mode 是 RelativeSource 的属性,用于设置绑定模式,属性值有 Self
和 TemplatedParent。当 Mode 属性取值为 Self 时,表示元素或元素的属性是绑定源;取值为
TemplatedParent 时,表示引用 ControlTemplate 模板的元素或元素的属性是绑定源。Path 也
是 RelativeSource 的属性,用于指定被绑定的属性
<targetObject targetProperty="{Binding}" ...> ... </targetObject>
或
<targetObject targetProperty="{Binding [Path=]bindingSourceProperty}" ...> ... </
targetObject>
2. StaticResource
<object propertyName="{StaticResource resourceKey}" ...> ... </ object>
resourceKey 是被引用的资源的键,由资源定义时的 x:Key 或 x:Name 指定
3. TemplateBinding
<templateObject templateProperty="{TemplateBinding [Property=] sourceObjectProperty}"
...> ... </templateObject>
templateObject 是 模 板 对 象 ;templateProperty 是 模 板 对 象 的 属 性 ;Property
是 TemplateBinding 的 属 性, 用 于 指 定 被 绑 定 对 象 的 属 性, 通 常 可 省 略 不 写 ;
sourceObjectProperty 是被绑定对象的属性。
4. RelativeSource
TemplatedParent 模式通常使用于 ControlTemplate 元素定义中
<object property="{Binding RelativeSource={RelativeSource [Mode=]modeValue}}" ...>
... </object>
或
<object property="{Binding RelativeSource={RelativeSource [Mode=]
modeValue},[Path=]bindedProperty}" ...> ... </object>
Mode 是 RelativeSource 的属性,用于设置绑定模式,属性值有 Self
和 TemplatedParent。当 Mode 属性取值为 Self 时,表示元素或元素的属性是绑定源;取值为
TemplatedParent 时,表示引用 ControlTemplate 模板的元素或元素的属性是绑定源。Path 也
是 RelativeSource 的属性,用于指定被绑定的属性
本文深入探讨了WPF中数据绑定的基本概念,包括Binding、StaticResource、TemplateBinding和RelativeSource,并详细解释了它们的应用场景和区别。同时介绍了如何在WPF中灵活使用这些特性来实现高效的数据展示和交互。

被折叠的 条评论
为什么被折叠?



