WPF绑定数据源之RelativeSource
Command="{Binding ConfirmRegisterCommand}" CommandParameter="{Binding RelativeSource={x:Static RelativeSource.Self},Path=DataContext}"
一.FindAncestor
1、XAML
Html代码:
<Grid x:Name="g1" Background="Red" Margin="10">
<DockPanel x:Name="d1" Background="Orange" Margin="10">
<Grid x:Name="g2" Background="Yellow" Margin="10">
<DockPanel x:Name="d2" Background="LawnGreen" Margin="10">
<TextBox x:Name="textBox1" FontSize="24" Margin="10"/>
</DockPanel>
</Grid>
</DockPanel>
</Grid>
2、后台代码
Csharp代码 :
RelativeSource rs = new RelativeSource(RelativeSourceMode.FindAncestor);
//设定为离自己控件最近的第二层父控件
rs.AncestorLevel = 2;
//设定父控件为Gird类型
rs.AncestorType = typeof(Grid);
//绑定源为Grid的名称
Binding binding = new Binding("Name") { RelativeSource=rs};
//将绑定的源放在文本显示内容中
this.textBox1.SetBinding(TextBox.TextProperty, binding);
3、以上后台代码等同于XAML中的
Html代码:
<TextBox x:Name="textBox1" FontSize="24" Margin="10" Text="{Binding RelativeSource={RelativeSource FindAncestor,AncestorType={x:Type Grid},AncestorLevel=2},Path=Name}"/>
二.TemplatedParent
TemplatedParent是RelativeSource的其中一种方式,使用该方式将使源元素成为模板目标类型—即TargetType;如果该绑定是在模板中,那么它的作为范围也只限于该模板.
例:
<Style TargetType="{x:Type local:TemplatedParent}">
<Setter Property="Background" Value="Green"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type local:TemplatedParent}">
<Grid>
<Ellipse>
<Ellipse.Fill>
<SolidColorBrush Color="{Binding Path=Background.Color,RelativeSource={RelativeSource TemplatedParent}}"/>
</Ellipse.Fill>
</Ellipse>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
这样绑定的源元素就指向local:TemplatedParent这个目标类型了,所以当你修改目标类型的背景颜色时,Ellipse也将跟随它变化。
三.Self
<Window x:Class="WpfApplication1.chap5_2"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="chap5_2" Height="300" Width="300">
<Grid>
<Slider Name="slider"
Margin="4" Interval="1"
TickFrequency="1"
IsSnapToTickEnabled="True"
Minimum="0" Maximum="100"
ToolTip="{Binding RelativeSource ={ RelativeSource Self}, Path=Value}"/>
</Grid>
</Window>
其中Binding RelativeSource={RelativeSource Self}等价于Binding RelativeSource={x:Static RelativeSource.Self}
![]() |
WPF绑定数据源之RelativeSource的更多相关文章
- WPF绑定数据源
using System;using System.Collections.Generic;using System.Collections.ObjectModel;using System.Comp ...
- WPF利用通过父控件属性来获得绑定数据源RelativeSource
WPF利用通过父控件属性来获得绑定数据源RelativeSource 有时候我们不确定作为数据源的对象叫什么名字,但知道作为绑定源与UI布局有相对的关系,如下是一段XAML代码,说明多层布局控件中 ...
- 在WPF中如何使用RelativeSource绑定
在WPF绑定的时候,指定绑定源时,有一种办法是使用RelativeSource. 这种办法的意思是指当前元素和绑定源的位置关系. 第一种关系: Self 举一个最简单的例子:在一个StackPanel ...
- WPF 绑定以基础数据类型为集合的无字段名的数据源
WPF 绑定以基础数据类型为集合的无字段名的数据源 运行环境:Window7 64bit,.NetFramework4.61,C# 6.0: 编者:乌龙哈里 2017-02-21 我们在控件的数据绑定 ...
- WPF绑定各种数据源之object数据源
一.WPF绑定各种数据源索引 WPF 绑定各种数据源之Datatable WPF绑定各种数据源之object数据源 WPF绑定各种数据源之xml数据源 WPF绑定各种数据源之元素控件属性 Bindin ...
- WPF学习笔记——ListBox用ItemsSource绑定数据源
作为一个WPF初学者,感到困难重重.在网上想查个ListBox绑定数据源的示例,结果出来一大堆代码,一看心就烦. 我给个简洁一点的代码: 后台代码: protected class UserItem ...
- WPF绑定各种数据源之xml数据源
一.WPF绑定各种数据源索引 WPF 绑定各种数据源之Datatable WPF绑定各种数据源之object数据源 WPF绑定各种数据源之xml数据源 WPF绑定各种数据源之元素控件属性 Bindin ...
- WPF绑定のRelativeSource
在WPF绑定的时候,指定绑定源时,有一种办法是使用RelativeSource. 这种办法的意思是指当前元素和绑定源的位置关系. 第一种关系: Self 举一个最简单的例子:在一个StackPanel ...
- WPF DevExpress Chart控件 界面绑定数据源,不通过C#代码进行绑定
<Grid x:Name="myGrid" Loaded="Grid_Loaded" DataContext="{Binding PartOne ...
随机推荐
- add jars、add external jars、add library、add class folder的区别
add external jars = 增加工程外部的包add jars = 增加工程内包add library = 增加一个库add class folder = 增加一个类文件夹 add jar是 ...
- XSL-FO Page Layout
Simple Layout Let's take a look at the simple page layout that we saw earlier in the course. The sim ...
- leetCode 64.Minimum Path Sum (最短路) 解题思路和方法
Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which ...
- jQuery 查找标签
1 基本选择器 2 基本筛选器 3 属性选择器 4 间接选择 1 基本选择器 //id选择器: $("#id") //标签选择器: $("tagName") / ...
- 修改Linux的基本配置(修改主机名修改ip地址安装JDK/Tomcat/MySQL等等)
(一)基本操作修改 修改主机名 vi /etc/sysconfig/network NETWORKING=yes HOSTNAME=server1.itcast.cn 修改ip地址 vi /etc/s ...
- Android studio 相关下载
Android studio http://www.androiddevtools.cn/ Oracle的VirtulBox https://www.virtualbox.org/wiki/Down ...
- java ReentrantLock可重入锁的使用场景
摘要 从使用场景的角度出发来介绍对ReentrantLock的使用,相对来说容易理解一些. 场景1:如果发现该操作已经在执行中则不再执行(有状态执行) a.用在定时任务时,如果任务执行时间可能超过下次 ...
- A除以B (20)
时间限制 100 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue 本题要求计算A/B,其中A是不超过1000位的正整数,B是1位正整 ...
- PL/SQL不能格式化SQL:--PL/SQL Beautifier could not parse text
PL/SQL sql语句美化器点击没有反应.查看下面提示PL/SQL Beautifier could not parse text.本人此次产生的原因是sql语句语法错误. 工具栏处(如果没有此按钮 ...
- LeetCode:最长公共前缀【14】
LeetCode:最长公共前缀[14] 题目描述 编写一个函数来查找字符串数组中的最长公共前缀. 如果不存在公共前缀,返回空字符串 "". 示例 1: 输入: ["flo ...