RelativeSource 简述
RelativeSource实现标记扩展,以描述绑定源相对于绑定目标的位置。
<Binding><Binding.RelativeSource><RelativeSource Mode="modeEnumValue"/></Binding.RelativeSource> </Binding> - or <Binding><Binding.RelativeSource><RelativeSourceMode="FindAncestor"AncestorType="{x:Type typeName}"AncestorLevel="intLevel"/></Binding.RelativeSource> </Binding>
// Summary:// Describes the location of the binding source relative to the position of// the binding target.public enum RelativeSourceMode{// Summary:// Allows you to bind the previous data item (not that control that contains// the data item) in the list of data items being displayed.PreviousData = 0,//// Summary:// Refers to the element to which the template (in which the data-bound element// exists) is applied. This is similar to setting a System.Windows.TemplateBindingExtension// and is only applicable if the System.Windows.Data.Binding is within a template.TemplatedParent = 1,//// Summary:// Refers to the element on which you are setting the binding and allows you// to bind one property of that element to another property on the same element.Self = 2,//// Summary:// Refers to the ancestor in the parent chain of the data-bound element. You// can use this to bind to an ancestor of a specific type or its subclasses.// This is the mode you use if you want to specify System.Windows.Data.RelativeSource.AncestorType// and/or System.Windows.Data.RelativeSource.AncestorLevel.FindAncestor = 3,}
Xaml 示例
<Window.Resources><ControlTemplate x:Key="template"><Canvas><Canvas.RenderTransform><RotateTransform Angle="20"/></Canvas.RenderTransform><Ellipse Height="100" Width="150" Fill="{Binding RelativeSource={RelativeSource TemplatedParent},Path=Background}"></Ellipse><ContentPresenter Margin="35" Content="{Binding RelativeSource={RelativeSource TemplatedParent},Path=Content}"/></Canvas></ControlTemplate></Window.Resources><StackPanel><TextBlock><TextBlock.Text><Binding Path="Title"><Binding.RelativeSource><RelativeSource Mode="FindAncestor" AncestorType="{x:Type Window}" /></Binding.RelativeSource></Binding></TextBlock.Text></TextBlock><TextBlock Text="{Binding Path=Title,RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}} }"></TextBlock> <Button Template="{StaticResource template}" Background="AliceBlue"><TextBlock FontSize="22">Click me</TextBlock></Button></StackPanel>
RelativeSource内部实现
using System; using System.ComponentModel; using System.Windows.Markup;namespace System.Windows.Data { public class RelativeSource : MarkupExtension, ISupportInitialize{ public RelativeSource();public RelativeSource(RelativeSourceMode mode);public RelativeSource(RelativeSourceMode mode, Type ancestorType, int ancestorLevel);public Type AncestorType { get; set; } public RelativeSourceMode Mode { get; set; }public static RelativeSource PreviousData { get; }public static RelativeSource Self { get; }public static RelativeSource TemplatedParent { get; }public override object ProvideValue(IServiceProvider serviceProvider);...} }
using System; using System.ComponentModel; using System.Windows.Markup;namespace System.Windows.Data {/// <summary>Implements a markup extension that describes the location of the binding source relative to the position of the binding target.</summary>[MarkupExtensionReturnType(typeof(RelativeSource))]public class RelativeSource : MarkupExtension, ISupportInitialize{private RelativeSourceMode _mode;private Type _ancestorType;private int _ancestorLevel = -1;private static RelativeSource s_previousData;private static RelativeSource s_templatedParent;private static RelativeSource s_self;/// <summary>Gets a static value that is used to return a <see cref="T:System.Windows.Data.RelativeSource" /> constructed for the <see cref="F:System.Windows.Data.RelativeSourceMode.PreviousData" /> mode.</summary>/// <returns>A static <see cref="T:System.Windows.Data.RelativeSource" />.</returns>public static RelativeSource PreviousData{get{if (RelativeSource.s_previousData == null){RelativeSource.s_previousData = new RelativeSource(RelativeSourceMode.PreviousData);}return RelativeSource.s_previousData;}}/// <summary>Gets a static value that is used to return a <see cref="T:System.Windows.Data.RelativeSource" /> constructed for the <see cref="F:System.Windows.Data.RelativeSourceMode.TemplatedParent" /> mode.</summary>/// <returns>A static <see cref="T:System.Windows.Data.RelativeSource" />.</returns>public static RelativeSource TemplatedParent{get{if (RelativeSource.s_templatedParent == null){RelativeSource.s_templatedParent = new RelativeSource(RelativeSourceMode.TemplatedParent);}return RelativeSource.s_templatedParent;}}/// <summary>Gets a static value that is used to return a <see cref="T:System.Windows.Data.RelativeSource" /> constructed for the <see cref="F:System.Windows.Data.RelativeSourceMode.Self" /> mode.</summary>/// <returns>A static <see cref="T:System.Windows.Data.RelativeSource" />.</returns>public static RelativeSource Self{get{if (RelativeSource.s_self == null){RelativeSource.s_self = new RelativeSource(RelativeSourceMode.Self);}return RelativeSource.s_self;}}/// <summary>Gets or sets a <see cref="T:System.Windows.Data.RelativeSourceMode" /> value that describes the location of the binding source relative to the position of the binding target.</summary>/// <returns>One of the <see cref="T:System.Windows.Data.RelativeSourceMode" /> values. The default value is null.</returns>/// <exception cref="T:System.InvalidOperationException">This property is immutable after initialization. Instead of changing the <see cref="P:System.Windows.Data.RelativeSource.Mode" /> on this instance, create a new <see cref="T:System.Windows.Data.RelativeSource" /> or use a different static instance.</exception>[ConstructorArgument("mode")]public RelativeSourceMode Mode{ get{return this._mode;}set{if (this.IsUninitialized){this.InitializeMode(value);return;}if (value != this._mode){throw new InvalidOperationException(SR.Get("RelativeSourceModeIsImmutable"));}}}/// <summary>Gets or sets the type of ancestor to look for.</summary>/// <returns>The type of ancestor. The default value is null.</returns>/// <exception cref="T:System.InvalidOperationException">The <see cref="T:System.Windows.Data.RelativeSource" /> is not in the <see cref="F:System.Windows.Data.RelativeSourceMode.FindAncestor" /> mode.</exception>public Type AncestorType{ get{return this._ancestorType;}set{if (this.IsUninitialized){this.AncestorLevel = 1;}if (this._mode != RelativeSourceMode.FindAncestor){if (value != null){throw new InvalidOperationException(SR.Get("RelativeSourceNotInFindAncestorMode"));}}else{this._ancestorType = value;}}}/// <summary>Gets or sets the level of ancestor to look for, in <see cref="F:System.Windows.Data.RelativeSourceMode.FindAncestor" /> mode. Use 1 to indicate the one nearest to the binding target element.</summary>/// <returns>The ancestor level. Use 1 to indicate the one nearest to the binding target element.</returns>public int AncestorLevel{ get{return this._ancestorLevel;}set{if (this._mode != RelativeSourceMode.FindAncestor){if (value != 0){throw new InvalidOperationException(SR.Get("RelativeSourceNotInFindAncestorMode"));}}else{if (value < 1){throw new ArgumentOutOfRangeException(SR.Get("RelativeSourceInvalidAncestorLevel"));}this._ancestorLevel = value;}}}private bool IsUninitialized{get{return this._ancestorLevel == -1;}}/// <summary>Initializes a new instance of the <see cref="T:System.Windows.Data.RelativeSource" /> class.</summary>public RelativeSource(){this._mode = RelativeSourceMode.FindAncestor;}/// <summary>Initializes a new instance of the <see cref="T:System.Windows.Data.RelativeSource" /> class with an initial mode.</summary>/// <param name="mode">One of the <see cref="T:System.Windows.Data.RelativeSourceMode" /> values.</param>public RelativeSource(RelativeSourceMode mode){this.InitializeMode(mode);}/// <summary>Initializes a new instance of the <see cref="T:System.Windows.Data.RelativeSource" /> class with an initial mode and additional tree-walking qualifiers for finding the desired relative source.</summary>/// <param name="mode">One of the <see cref="T:System.Windows.Data.RelativeSourceMode" /> values. For this signature to be relevant, this should be <see cref="F:System.Windows.Data.RelativeSourceMode.FindAncestor" />.</param>/// <param name="ancestorType">The <see cref="T:System.Type" /> of ancestor to look for.</param>/// <param name="ancestorLevel">The ordinal position of the desired ancestor among all ancestors of the given type. </param>public RelativeSource(RelativeSourceMode mode, Type ancestorType, int ancestorLevel){this.InitializeMode(mode);this.AncestorType = ancestorType;this.AncestorLevel = ancestorLevel;}/// <summary>This member supports the Windows Presentation Foundation (WPF) infrastructure and is not intended to be used directly from your code.</summary>void ISupportInitialize.BeginInit(){}///<summary>This member supports the Windows Presentation Foundation (WPF) infrastructure and is not intended to be used directly from your code.</summary>void ISupportInitialize.EndInit(){if (this.IsUninitialized){throw new InvalidOperationException(SR.Get("RelativeSourceNeedsMode"));}if (this._mode == RelativeSourceMode.FindAncestor && this.AncestorType == null){throw new InvalidOperationException(SR.Get("RelativeSourceNeedsAncestorType"));}}/// <summary>Indicates whether the <see cref="P:System.Windows.Data.RelativeSource.AncestorType" /> property should be persisted.</summary>/// <returns>true if the property value has changed from its default; otherwise, false.</returns> public bool ShouldSerializeAncestorType(){return this._mode == RelativeSourceMode.FindAncestor;}/// <summary>Indicates whether the <see cref="P:System.Windows.Data.RelativeSource.AncestorLevel" /> property should be persisted.</summary>/// <returns>true if the property value has changed from its default; otherwise, false.</returns> public bool ShouldSerializeAncestorLevel(){return this._mode == RelativeSourceMode.FindAncestor;}/// <summary>Returns an object that should be set as the value on the target object's property for this markup extension. For <see cref="T:System.Windows.Data.RelativeSource" />, this is another <see cref="T:System.Windows.Data.RelativeSource" />, using the appropriate source for the specified mode. </summary>/// <returns>Another <see cref="T:System.Windows.Data.RelativeSource" />.</returns>/// <param name="serviceProvider">An object that can provide services for the markup extension. In this implementation, this parameter can be null.</param>public override object ProvideValue(IServiceProvider serviceProvider){if (this._mode == RelativeSourceMode.PreviousData){return RelativeSource.PreviousData;}if (this._mode == RelativeSourceMode.Self){return RelativeSource.Self;}if (this._mode == RelativeSourceMode.TemplatedParent){return RelativeSource.TemplatedParent;}return this;}private void InitializeMode(RelativeSourceMode mode){if (mode == RelativeSourceMode.FindAncestor){this._ancestorLevel = 1;this._mode = mode;return;}if (mode == RelativeSourceMode.PreviousData || mode == RelativeSourceMode.Self || mode == RelativeSourceMode.TemplatedParent){this._ancestorLevel = 0;this._mode = mode;return;}throw new ArgumentException(SR.Get("RelativeSourceModeInvalid"), "mode");}} }
RelativeSource 简述相关推荐
- Windows Presentation Foundation (WPF)中的命令(Commands)简述
Windows Presentation Foundation (WPF)中的命令(Commands)简述 原文:Windows Presentation Foundation (WPF)中的命令(C ...
- 简述计算机科学的核心内容,北京大学-计算机科学与技术(2018秋)作业及复习
59.(第十章)外排序是指在排序前后,数据在外存上,排序时数据调入内存进行的排序方法. 60.(第十章)在选择排序.冒泡排序.归并排序中, 归并排序是空间复杂度最大的. 三.简答和程序题(共10题,每 ...
- 设计模式学习1:设计模式简述和设计模式原则
设计模式简述 什么是设计模式? 软件工程中,设计模式(design pattern)是对软件设计中普遍存在(反复出现)的各种问题,所提出的解决方案. 设计模式的目的: 代码高可用(相同作用的代码能重复 ...
- Java中常见的锁简述
在Java的应用中,或多或少的都会接触到一些锁,那么问题就来了,在Java中,常见的锁有哪些,都有什么样的作用?? 这里给大家简单的简述一下这些学常见的锁. 本文件所涉及到的锁: 1.公平锁 / 非公 ...
- 简述DataInputStream和DataOuputStream
2019独角兽企业重金招聘Python工程师标准>>> Java开发中经常会用到IO流,那么就会碰到DataInputStream和DataOuputStream这两个包装类.下面就 ...
- 简要叙述matlab的含义,1,简述MATLAB组成部分? 2,说明使用M文件编辑/调试器的方法和优点? 3,存储在工作空间中的数组能编辑吗...
匿名用户 1级 2012-05-17 回答 我也考这个....祝你好运 1,简述MATLAB组成部分? (1)开发环境(development Environment); (2)MATLAB数学函数库 ...
- 简述Linux和Windows下Python搭建步骤
简述就Windows和Linux环境下安装Python的步骤. Python环境搭建首先到官网(www.python.org)下载相应的安装版本.主要分为Windows和Linux两种: 一.Linu ...
- 简述 OAuth 2.0 的运作流程
本文将以用户使用 github 登录网站留言为例,简述 OAuth 2.0 的运作流程. 假如我有一个网站,你是我网站上的访客,看了文章想留言表示「朕已阅」,留言时发现有这个网站的帐号才能够留言,此时 ...
- 简述机器指令与微指令之间的关系_自考《计算机组成原理》模拟试题(一)
一.单项选择题(本大题共 25小题,每小题1分,共25分)在每小题列出的四个选项中只有一个选项是符合题目要求的,请将正确选项前的字母填在题后的括号内. 1.-0的8位二进制补码是( ) A.10000 ...
最新文章
- js正则表达式/replace替换变量方法
- Python 三元条件判断表达式(and or/if else)
- 皮克斯首款VR体验《寻梦环游记》登陆 Oculus Rift
- 利用vgg预训练模型提取图像特征
- 小米4公布会视频地址
- Operating System Concepts--chap9 Memory Management;
- Python语言防坑小技巧
- VTK:PolyData之QuantizePolyDataPoints
- [转贴] 软件测试职业发展的 A 面和 B 面
- android系统 wifi,Android系统wifi分析-手动连接过程
- ubuntu 关闭IPv6
- 图像处理六:预处理方法
- vue上传图片文件到服务器,vue如何将quill图片上传到服务器
- nginx-配置记录
- 用Java写一个浪费cpu的程序_Java程序是如何浪费内存的
- ocr 识别 github 源码
- 计算机电脑桌面怎么设置,老司机教你电脑屏保怎么设置
- windows 批量创建文件夹
- 机器学习(周志华)学习笔记(二)
- WPS的标题样式如何保存成默认