RelativeSource有四种类型

news/2024/10/22 18:26:43/

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这个用得很少,表示值相对于以前数据的变化。

最终效果图

 

 


http://www.ppmy.cn/news/1000575.html

相关文章

【搜索】DFS搜索顺序

算法提高课笔记 目录 马走日题意思路代码 单词接龙题意思路代码 分成互质组题意思路代码 马走日 原题链接 马在中国象棋以日字形规则移动。 请编写一段程序&#xff0c;给定 n∗m 大小的棋盘&#xff0c;以及马的初始位置 (x&#xff0c;y)&#xff0c;要求不能重复经过棋盘…

Python的字典使用

今天做力扣上1207. 独一无二的出现次数添加链接描述时用到了python字典&#xff0c;于是把字典的用法整理了一下。 新建字典 iters {}检查字典中是否含有某一个键 iters.has_key(key)字典根据键访问值 iters[key]遍历字典的键和值 for key,value in iters.items():整体代码 c…

Ubuntu20.04之VNC的安装与使用

本教程适用于Ubuntu20.04及以下版本&#xff0c;Ubuntu22.04版本或有出入 更多更新的文章详见我的个人博客&#xff1a;【前往】 文章目录 1.安装图形桌面1.1选择安装gnome桌面1.2选择安装xface桌面 2.安装VNC-Server3.配置VCN-Server4.连接VNC5.设置VNC-Server为系统服务&…

【数据结构】二叉树、二叉搜索树、平衡二叉树、红黑树、B树、B+树

概述 二叉树&#xff08;Binary Tree&#xff09;&#xff1a;每个节点最多有两个子节点&#xff08;左子节点和右子节点&#xff09;&#xff0c;没有限制节点的顺序。特点是简单直观&#xff0c;易于实现&#xff0c;但查找效率较低。 二叉搜索树&#xff08;Binary Search…

nmake编译Qt第三方库出现无法打开包含文件type_traits

最近需要为个人项目ShaderLab添加内嵌的代码编辑窗口功能&#xff0c;支持语法高亮和Intellisense&#xff0c;最初使用了QCodeEditor,发现这个第三方的库对词法分析的实现效果不太行. 代码换行后直接缩进到首行&#xff0c;无法定位到前一句的首行 考虑换QScintilla&#xff…

容器化的好处

容器化&#xff0c;是指使用容器技术&#xff08;Docker/containerd等&#xff09;运行应用程序&#xff08;容器&#xff09;&#xff0c;并使用容器编排技术&#xff08;例如 K8s&#xff09;来管理这些容器。 我在之前的文章 《使用 Dockerfile 构建生产环境镜像》 提及普通…

Flink DataStream API详解

DataStream API 参考&#xff1a;https://ci.apache.org/projects/flink/flink-docs-release-1.9/dev/datastream_api.html Data Sources Source是程序读取其输入的位置&#xff0c;您可以使用env.addSource&#xff08;sourceFunction&#xff09;将Source附加到程序中。Fl…

随机森林算法深入浅出

随机森林&#xff08;Random Forest&#xff09;是一种集成学习&#xff08;Ensemble Learning&#xff09;算法&#xff0c;由于其优秀的表现在数据挖掘、机器学习等领域得到广泛应用。随机森林通过同时使用多个决策树对数据集进行训练&#xff0c;并通过投票机制或平均化方式…