wpf RelativeSource绑定
RelativeSource有四种类型
Self
FindAncestor
TemplatedParent
PreviousData
a.Self
Self用于绑定源和绑定目标相同的场景中。对象的一个属性与同一对象的另一个属性绑定。
例如,让我们取一个高度和宽度相同的椭圆。在XAML文件中添加下面给出的代码。宽度属性与高度属性相对绑定。
<Grid>
<Ellipse Width="{Binding RelativeSource={RelativeSource Self}, Path=Height}"
Height="100"
Fill="Black" />
</Grid>
b.FindAncestor
FindAncestor
顾名思义,当绑定源是绑定目标的祖先(父级)之一时使用此选项。使用FindAncestor扩展,可以找到任何级别的祖先。
现在,让我们使用FindAncestor扩展将祖先的Name属性绑定到子元素button的Content属性。
<Grid Name="Parent_3">
<StackPanel Name="Parent_222"
Width="100"
HorizontalAlignment="Center"
VerticalAlignment="Center">
<StackPanel Name="Parent_2"
Width="100"
HorizontalAlignment="Center"
VerticalAlignment="Center">
<Border Name="Parent_1">
<StackPanel x:Name="Parent_0"
Orientation="Vertical">
<!-- 下面这个按钮Content得到:Parent_2 -->
<Button Height="50"
Content="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type StackPanel}, AncestorLevel=2}, Path=Name}" />
<!-- 下面这个按钮Content得到:Parent_0 -->
<Button Height="50"
Content="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type StackPanel}, AncestorLevel=1}, Path=Name}" />
<!-- 下面这个按钮Content得到:Parent_0 -->
<Button Height="50"
Content="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type StackPanel}}, Path=Name}" />
</StackPanel>
</Border>
</StackPanel>
</StackPanel>
</Grid>
c.TemplatedParent
TemplatedParent是一个属性,它使您能够创建一个包含少量未知值的控件模板。这些值取决于应用ControlTemplate的控件的属性。
<Window.Resources>
<ControlTemplate x:Key="template1">
<!--
在应用模板时,按钮的Background(Beige)与椭圆的Fill属性相对绑定,Content(Click me)与ContentPresenter的Content属性相对绑定。依赖值生效并给出以下输出。
-->
<Canvas>
<Ellipse Width="155"
Height="110"
Fill="Black" />
<Ellipse Width="150"
Height="100"
Fill="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Background}" />
<ContentPresenter Margin="35"
Content="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Content}" />
</Canvas>
</ControlTemplate>
</Window.Resources>
<Button Height="0"
Margin="5"
Background="Beige"
Content="Click me"
FontSize="18"
Template="{StaticResource template1}" />
d.PreviousData
PreviousData这个用得很少,表示值相对于以前数据的变化。
wpf RelativeSource绑定的更多相关文章
- 学习WPF——元素绑定
概念 从源对象提取一些信息,并用这些信息设置目标对象的属性 示例 在给TextBlock控件的FontSize属性赋值时,我们使用了绑定表达式 数据绑定表达式使用XAML的标记扩展(因此具有花括号)( ...
- WPF 模板绑定父级控件内容
WPF 模板绑定父级控件内容 <Style TargetType="Button" x:Key="btn"> <Setter Property ...
- WPF元素绑定
原文:WPF元素绑定 数据绑定简介:数据绑定是一种关系,该关系告诉WPF从源对象提取一些信息,并用这些信息设置目标对象的属性.目标属性是依赖项属性.源对象可以是任何内容,从另一个WPF元素乃至ADO. ...
- 【WPF】最近在学习wpf 的绑定,,
最近在学习wpf 的绑定,,1.简单的说就是版前端和后端用自己的方法给分开了2.baseVm 模型 baseCmd 命令3.命令传参修改的只是界面里的属性,而不修改其它的值4.前端改变后端, 后端改变 ...
- 背水一战 Windows 10 (19) - 绑定: TemplateBinding 绑定, 与 RelativeSource 绑定, 与 StaticResource 绑定
[源码下载] 背水一战 Windows 10 (19) - 绑定: TemplateBinding 绑定, 与 RelativeSource 绑定, 与 StaticResource 绑定 作者:we ...
- wpf直接绑定xml生成应用程序
目的:在vs2010下用wpf完成一个配置工具,配置文件为xml格式,xml文件作为数据源,直接和wpf前台绑定,生成exe后,运行exe能够加载同路径下的xml配置文件并显示 xml文件在项目中的设 ...
- 绑定: TemplateBinding 绑定, 与 RelativeSource 绑定, 与 StaticResource 绑定
介绍背水一战 Windows 10 之 绑定 TemplateBinding 绑定 与 RelativeSource 绑定 与 StaticResource 绑定 示例1.演示 TemplateBin ...
- 重新想象 Windows 8 Store Apps (52) - 绑定: 与 Element Model Indexer Style RelativeSource 绑定, 以及绑定中的数据转换
[源码下载] 重新想象 Windows 8 Store Apps (52) - 绑定: 与 Element Model Indexer Style RelativeSource 绑定, 以及绑定中的数 ...
- WPF DataGrid绑定一个组合列
WPF DataGrid绑定一个组合列 前台: <Page.Resources> <local:InfoConverter x:Key="converter& ...
- WPF DataGrid 绑定行双击行命令
WPF DataGrid 绑定行双击行命令 <DataGrid ...> <DataGrid.InputBindings> <MouseBinding MouseActi ...
随机推荐
- springBoot 这货特别火
现在 Spring Boot 非常火,各种技术文章,各种付费教程,多如牛毛,可能还有些不知道 Spring Boot 的,那它到底是什么呢?有什么用?今天给大家详细介绍一下. Spring Boot ...
- PHP操作MySQL批量Update的写法,各框架通用防注入版
使用别人的扩展遇到了问题,发现没有做SQL注入的处理.我又写了个轮子,根据自己需求扩展了下,有需要的小伙伴可以直接取用. 这里就直接粘贴源码了,会用PHPD ,基本都会如何把它运用到各个框架里的. 本 ...
- cf823div2C
cf823div2C 题目链接 题目 给你两个字符串\(s_1,s_2\).每次操作可以让\(s_1\)的前k个和\(s_2\)的后k个交换.询问是否可以通过多次上述操作,使得\(s_1=s_2\). ...
- Spring 笔记三 AOP
1.AOP 概述 AOP (Aspect-Oriented Programming,面向切面编程):是一种新的方法论,是对传统 OOP (Object-Oriented Programming,面 ...
- IT工具知识-11:一种安卓投屏到Win10失败的解决方法
软硬件平台 电脑:WIN10 LTSC 手机:红米K30Pro/MIUI 11.0.26 投屏软件:安卓端-自带投屏,WIN10-自带投屏(连接) 故障描述 之前还能用的,但是在换了个路由器之后就不能 ...
- Delphi 关于RichEdit URL 颜色相关总结
一.代码改变字体大小和颜色 1 procedure TForm1.Button1Click(Sender: TObject); 2 var 3 sNickName, sstr: string; 4 b ...
- Python MySQLdb连接被多线程共享引发的内核segfault段错误
Python celery Worker exited prematurely: signal 11 (SIGSEGV) --一种解决方案 Python libmysqlclient segfault ...
- java map重写 转大写、转驼峰
/** * @author admin * @Description * 转大写 */ public class HashMapUpper<V> extends HashMap<St ...
- Tomcat源码部署
1.下载源码 https://dlcdn.apache.org/tomcat/tomcat-9/v9.0.54/src/apache-tomcat-9.0.54-src.zip 2.解压 3.在解压后 ...
- 标题Ubuntu将默认的python3改为默认的python
ryzen@cp7:~$ python Command 'python' not found, did you mean: command 'python3' from deb python3 roo ...