博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
WPF之Binding基础十 使用RelativeSource作为Binding的源
阅读量:6885 次
发布时间:2019-06-27

本文共 1264 字,大约阅读时间需要 4 分钟。

  当我们想未一个控件绑定值的时候,我们需要将某个控件的数据绑定到指定的控件上,就可以用RelativeSource。

XAML代码

<Window x:Class="使用Binding的RelativeSource.MainWindow"

xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid x:Name="第一层" Background="LightBlue" Margin="5">
<DockPanel Name="第二层" Background="Yellow" Margin="10">
<Grid x:Name="第三层" Background="Green" Margin="20">
<!--方案一,用cs-->
<!--<TextBox x:Name="zuihou" FontSize="20" Margin="10"/>-->
<!--方案二-->
<TextBox Text="{Binding RelativeSource={RelativeSource FindAncestor,AncestorType={x:Type Grid},AncestorLevel=1},Path=Name}"/>
</Grid>
</DockPanel>
</Grid>
</Window>

CS代码

namespace 使用Binding的RelativeSource

{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
//这是第一种方法
//RelativeSource rs = new RelativeSource(RelativeSourceMode.FindAncestor);
AncestorLevel是指以Binding目标控件为起点的目标偏移量
//rs.AncestorLevel = 1;
AncestorType是说Bingding目标的类型
//rs.AncestorType = typeof(Grid);
//Binding binding = new Binding("Name") { RelativeSource=rs};
//this.zuihou.SetBinding(TextBox.TextProperty, binding);
}
}
}

转载于:https://www.cnblogs.com/lijin/p/3151049.html

你可能感兴趣的文章
链表的C语言实现之单链表的实现
查看>>
C/C++变量在内存中的位置以及初始化问题
查看>>
下拉菜单的实现
查看>>
ubuntu下apache和mysql的命令
查看>>
Matlab实现西格玛(Sigma)平滑滤波
查看>>
ie浏览器下载文件时出现中文乱码以及空格变为+
查看>>
nginx服务器,访问时如何不直接显示index.php,而是显示目录
查看>>
【无私分享:ASP.NET CORE 项目实战(第十章)】发布项目到 Linux 上运行 Core 项目...
查看>>
播放器授权后播放内容时出现Cnario logo水印
查看>>
【数据结构】重新面对数据结构
查看>>
CSS--字体|垂直居中|background
查看>>
开启了HA的XenServer如何关闭虚拟机?
查看>>
多重背包问题
查看>>
windows下java环境变量标准配置
查看>>
【转】CSS
查看>>
Monkey测试环境搭建
查看>>
SDUT 2137 数据结构实验之求二叉树后序遍历和层次遍历
查看>>
IOS开发 EaseMobSDK 环信快速集成
查看>>
okhttputils使用(zhuan)
查看>>
python:正则表达式
查看>>