WPF中Binding的技巧(一)
来源:互联网 发布:淘宝网积木玩具 编辑:程序博客网 时间:2024/05/21 10:22
在WPF应用的开发过程中Binding是一个非常重要的部分。
在实际开发过程中Binding的不同种写法达到的效果相同但事实是存在很大区别的。
这里将实际中碰到过的问题做下汇总记录和理解。
1. source = {binding} 和source = {binding RelativeSource={RelativeSource self},Path=DataContext}效果相同
理解:{binding} 不设定明确的绑定的source,这样binding就去从本控件类为开始根据可视树的层次结构自下而上查找不为空的Datacontext属性的值。
{binding RelativeSource={RelativeSource self},Path=DataContext}中RelativeSource self的含义为绑定的source为控件自身,这样binding 就绑定了自身控件的Datacontext。
效果:
<StackPanel DataContext="abc">
<Label Content="{Binding}"></Label>
<Label Content="{Binding RelativeSource={RelativeSource Self},Path=DataContext}"></Label>
</StackPanel>
<StackPanel DataContext="abc">
<Label Content="{Binding}"></Label>
<Label DataContext="def" Content="{Binding RelativeSource={RelativeSource Self},Path=DataContext}"></Label>
</StackPanel>
2.在Template的Trigger中改变Template中某个样式控件的属性
<Style TargetType="{x:Type Button}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Button}">
<Border>
<Label x:Name="PART_Label" Content="{TemplateBinding ContentA}" />
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsChecked" Value="True">
注: <Setter TargetName="PART_Label" Property="Content" Value="{Binding Path=ContentB, RelativeSource={RelativeSource TemplatedParent}}" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
当然把注:的这句改成<Setter TargetName="PART_Label" Property="Content" Value="{Binding Path=ContentB, RelativeSource={RelativeSourceAncestorType={x:Type Button}}}">效果一样。
先写到这,下篇继续关注Binding中ElementName,RelativeSource,Source的相同点和区别。
转载时,请注明本文来源:www.cnblogs.com/tmywu
- WPF中Binding的技巧(一)
- WPF中Binding的技巧(一)
- WPF中Binding的技巧(一)
- WPF的Binding学习笔记(一)
- WPF的Binding基础(一)
- WPF Binding相关的一些常见方式总结(一)
- WPF的Binding (path)学习笔记(一)
- WPF学习之Binding的学习(一)
- 深入浅出WPF之Binding的使用(一)
- WPF中的Binding技巧(二)
- WPF中的Binding技巧(二)
- WPF中的Binding技巧(二)
- C# WPF中DataGrid的数据绑定(Binding)
- WPF中的Binding(一)
- WPF中radiobutton 的 data binding方法
- WPF中的binding(九)- 使用Binding的RelativeSource
- WPF中的binding(十)- Binding数据的校验
- WPF中的binding(十一)- Binding数据的转换
- UML基础
- WPF中的Binding技巧(二)
- POJ 2635 The Embarrassed Cryptographer
- class反编译插件
- 网络基础学习笔记
- WPF中Binding的技巧(一)
- 专题1----从美国印钞看中国当前经济发展形势
- suse12.2硬盘安装的BUG。no repository found
- HDU 4283 You Are the One 区间dp
- EL表达式详解
- (十) 程序文件、预处理指令
- 算法导论中动态规划算法的简单实现
- 【Android API指南】App组件(8) - Services(2) - AIDL
- oracle中的db_file_multiblock_read_count参数