当前位置: 首页 > news >正文

wpf之RelativeSource用法总结

前言

RelativeSource用于在绑定时相对于当前元素来定位绑定源,而不是使用 ElementName 或 DataContext,它主要有两种相对于当前元素来确定绑定源的方式,一种是将自己作为绑定源;另一种是指定绑定的控件类型,系统自动寻找自身的父控件,父控件的父控件,以此类推一层层往上寻找。

1、self

在这里插入图片描述

下面的代码中Button的Width属性采用绑定的方式,并且指定Path为Height属性,然后使用RelativeSource={RelativeSource self}来指定绑定的源就是自己,所以你能看到最后Button的宽度和高度是一样大。

<Window x:Class="wpf之RelativeSource.MainWindow"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:d="http://schemas.microsoft.com/expression/blend/2008"xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"xmlns:local="clr-namespace:wpf之RelativeSource"mc:Ignorable="d"Title="MainWindow" Height="450" Width="800"><Grid Background="Blue"  ><Grid.RowDefinitions ><RowDefinition  Height="300"/><RowDefinition  Height=" 1*"/></Grid.RowDefinitions ><Grid Grid.Row="0" Background="Red" ><StackPanel Orientation="Vertical" ><Button  Height=" 100" Content=" 1"  Width="{Binding Path=Height, RelativeSource={RelativeSource self}}"/></StackPanel ></Grid></Grid >
</Window>

2、AncestorType

在这里插入图片描述
下面的代码中Button的Foreground 字体颜色属性采用绑定的方式,并且指定Path为Background 属性,然后第一个Button使用RelativeSource={RelativeSource AncestorType=Grid }来指定绑定的源的类型是Grid,这样系统就能自动寻找Button的父控件中第一个Grid的Background,最后也是为红色;第二个Button使用RelativeSource={RelativeSource AncestorType=Grid, AncestorLevel=2 }来指定绑定的源的类型是Grid,并且通过AncestorLevel=2来指定找到的第2个Grid才是最终要找的Grid,最后找到的Grid也就是最外层的Grid背景也就是蓝色,所以第2个Button的字体颜色就是蓝色。

<Window x:Class="wpf之RelativeSource.MainWindow"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:d="http://schemas.microsoft.com/expression/blend/2008"xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"xmlns:local="clr-namespace:wpf之RelativeSource"mc:Ignorable="d"Title="MainWindow" Height="450" Width="800"><Grid Background="Blue"  ><Grid.RowDefinitions ><RowDefinition  Height="300"/><RowDefinition  Height=" 1*"/></Grid.RowDefinitions ><Grid Grid.Row="0" Background="Red" ><StackPanel Orientation="Vertical" ><Button  Height="20"  Content=" 2"  Foreground ="{Binding Path=Background , RelativeSource={RelativeSource AncestorType=Grid }}"/><Button  Height=" 20"  Content=" 2"  Foreground ="{Binding Path=Background , RelativeSource={RelativeSource AncestorType=Grid, AncestorLevel=2 }}"/></StackPanel ></Grid></Grid >
</Window>

马工撰写的年入30万+C#上位机项目实战必备教程(点击下方链接即可访问文章目录)

1、《C#串口通信从入门到精通》
2、《C#与PLC通信从入门到精通 》
3、《C# Modbus通信从入门到精通》
4、《C#Socket通信从入门到精通 》
5、《C# MES通信从入门到精通》
6、《winform控件从入门到精通》
7、《C#操作MySql数据库从入门到精通》

http://www.dtcms.com/a/457093.html

相关文章:

  • 【C语言基础详细版】06. 动态内存管理:从原理到实战应用
  • 磁悬浮轴承转子不平衡质量控制深度解析
  • 关于力扣2025.10.8每日的收货
  • 烟台做网站的价格网络工程是冷门专业吗
  • 亲测可用,R语言 ggplot2 箱线图线条控制参数详解,箱线图离散数值控制
  • 沙漠风网站建设公司太原不错的互联网公司
  • 记录thinkphp模型查询时select与count执行顺序的疑问
  • AI编写的一个服务器监控源码
  • C# TCP 客户端开发笔记(TcpClient)
  • 网站建设数据库怎么弄个人养老金交15年领多少
  • Linux的Socket编程之TCP
  • ST-Raptor:无需微调,准确率超越 GPT-4o 的半结构化表格问答新范式
  • 深入洞察:华为BLM战略模型和BEM执行模型(图解)
  • wordpress跳转手机站wordpress 短代码 对齐
  • TNNLS-2022《Fast Incomplete Multi-view Clustering with View-independent Anchors》
  • 聊城网站开发培训公司网站可以免费建吗
  • 河南网站设计公司价格青岛企业网站seo技巧
  • 动态DP细谈
  • PHP Exception:深入理解与最佳实践
  • Nginx 反向代理与负载均衡
  • 学校网站建设是什么低价网站建设公司
  • 【HarmonyOS】窗口管理实战指南
  • Java基础加强13-集合框架、Stream流
  • 鸿蒙 hiperf 制作火焰图
  • 随机变量基础教程
  • 鞋子网站建设策划书北京网站设计多少钱
  • 超越传统:精密蚀刻如何实现±1μm的极致公差
  • 鸿蒙NEXT系列之鸿蒙NDK UI 初探
  • gRPC从0到1系列【25】
  • 冠县网站建设电话wordpress优化打开速度插件