将控件的当前实例绑定到一个附加属性

5

我想知道是否可以将用户控件或窗口的当前实例绑定到其xaml中定义的附加属性,例如:

<Window MyAttachedProp.Value="{Binding Self}"/>

您想让MyAttachedProp.Value具有Window对象引用吗? - Colin Smith
2个回答

4
你希望MyAttachedProp.Value拥有Window对象的引用?
你可以使用以下方法之一:
1. {Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}} 2. 给你的窗口一个x:Name="XXXXX",然后使用{Binding ElementName=XXXXX}找到它。 3. {Binding RelativeSource={RelativeSource Self}} 4. {Binding RelativeSource={x:Static RelativeSource.Self}} 使用示例4可避免创建新的RelativeSource对象(将Mode设置为Self)...而是指向在RelativeSource类中已经创建的静态对象....(这是一个非常微小和过早的优化)。
大多数人使用示例3,因为它更简洁易读。
参考链接:http://www.c-sharpcorner.com/uploadfile/yougerthen/relativesources-in-wpf/

1

{Binding RelativeSource={RelativeSource Self}}

的意思是“绑定到自身的相对源”。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接