【问题标题】:Bind the current instance of a control to an attached property将控件的当前实例绑定到附加属性
【发布时间】:2012-08-20 19:32:37
【问题描述】:

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

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

【问题讨论】:

  • 您希望 MyAttachedProp.Value 具有 Window 对象引用吗?

标签: wpf xaml binding


【解决方案1】:

您希望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,因为它的输入更少,阅读更清晰。

【讨论】:

    【解决方案2】:

    {Binding RelativeSource={RelativeSource Self}}

    【讨论】:

      猜你喜欢
      • 2019-07-11
      • 2011-10-03
      • 1970-01-01
      • 2011-02-07
      • 1970-01-01
      • 2013-03-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多