RelativeResource的四种模式

本文详细介绍了WPF中RelativeSourceMode的四种模式:Self、FindAncestor、TemplatedParent和PreviousData,并提供了相应的代码示例,展示了如何在实际场景中应用这些绑定模式,以实现特定的界面效果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 以下是MSDN中对public enum RelativeSourceMode的描述

FIELDS
FindAncestor 3

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 AncestorType and/or AncestorLevel.

PreviousData 0

Allows you to bind the previous data item (not that control that contains the data item) in the list of data items being displayed.

Self 2

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.

TemplatedParent 1

Refers to the element to which the template (in which the data-bound element exists) is applied. This is similar to setting a TemplateBindingExtension and is only applicable if the Binding is within a template.

 下面我将对这四种模式进行一 一的实现

Mode Self

  把自身的宽度绑定到自身的高度上面。

<Rectangle Fill="Red" Name="rectangle" 
                Height="100" Stroke="Black" 
                Canvas.Top="100" Canvas.Left="100"
                Width="{Binding ElementName=rec
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值