我想知道是否可以将用户控件或窗口的当前实例绑定到其xaml中定义的附加属性,例如:
<Window MyAttachedProp.Value="{Binding Self}"/>
MyAttachedProp.Value
拥有Window
对象的引用?{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
类中已经创建的静态对象....(这是一个非常微小和过早的优化)。{Binding RelativeSource={RelativeSource Self}}