wpf之RelativeSource用法总结

前言

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

1、self

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

csharp 复制代码
<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的字体颜色就是蓝色。

csharp 复制代码
<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数据库从入门到精通》

相关推荐
专注VB编程开发20年1 小时前
c#语法和java相差多少
java·开发语言·microsoft·c#
SmoothSailingT2 小时前
C#——Lazy<T>懒加载机制
开发语言·单例模式·c#·懒加载
czhc11400756632 小时前
c# 1216
windows·microsoft·c#
幸存者letp2 小时前
为什么 max(words, key=len) 中需要传 key=len
服务器·开发语言·c#
SmoothSailingT3 小时前
C#——Interface(接口)
开发语言·c#·接口
Henry_Wu0013 小时前
go与c# 及nats和rabbitmq交互
golang·c#·rabbitmq·grpc·nats
烛阴4 小时前
深入 C# 字符串世界:基础语法、常用方法与高阶实战
前端·c#
用户4488466710604 小时前
.NET进阶——深入理解线程(2)Thread入门到精通
c#·.net
没有bug.的程序员4 小时前
SOA、微服务、分布式系统的区别与联系
java·jvm·微服务·架构·wpf·日志·gc
民乐团扒谱机5 小时前
【微实验】基于Python实现的实时键盘鼠标触控板拾取检测(VS2019,附完整代码)
python·c#·计算机外设