【问题标题】:Binding to dependency property via PlacementTarget fails - wrong path syntax?通过 PlacementTarget 绑定到依赖属性失败 - 路径语法错误?
【发布时间】:2012-12-28 12:22:25
【问题描述】:

没问题

我创建了一个依赖属性,它接受一个字符串值。我将它设置在 TextBlock 上,它可以工作:

<TextBlock dp:ElementDataContext.ElementName="LvMain">

我验证了属性ElementDataContext.ElementName 设置为“LvMain”。

问题

现在问题来了:在TextBlock 的上下文菜单中,我想通过PlacementTarget 绑定到这个依赖属性。

这是我尝试的方法。这是我的 XAML 的摘录,其中包含 TextBlockContextMenu

<TextBlock dp:ElementDataContext.ElementName="LvMain">
    <TextBlock.ContextMenu>
        <ContextMenu Tag="{Binding PlacementTarget.(dp:ElementDataContext.ElementName), RelativeSource={RelativeSource Self}}">

这在运行时失败。打开上下文菜单时,它给了我一个“BindingExpression 路径错误”:

BindingExpression path error: '(dp:ElementDataContext.ElementName)' property not found on 'object' ''TextBlock' (Name='')'. BindingExpression:Path=PlacementTarget.(dp:ElementDataContext.ElementName); DataItem='ContextMenu' (Name='contextMenu'); target element is 'ContextMenu' (Name='contextMenu'); target property is 'Tag' (type 'Object')

我怀疑我的绑定路径有误。我试过了

  • PlacementTarget.(dp:ElementDataContext.ElementName)
  • PlacementTarget.dp:ElementDataContext.ElementName
  • PlacementTarget.ElementDataContext.ElementName

没有任何作用。什么是正确的语法?这甚至可能吗?

【问题讨论】:

标签: wpf data-binding dependency-properties


【解决方案1】:

属性路径语法PlacementTarget.(dp:ElementDataContext.ElementName)是正确的,但你还必须在属性表达式中显式编写Path=...部分:

<ContextMenu Tag="{Binding Path=PlacementTarget.(dp:ElementDataContext.ElementName),
                           RelativeSource={RelativeSource Self}}">

但是,Binding Markup Extension 中的 Implicit Path 部分并未提及此行为。

【讨论】:

  • 纳尔夫。就是这样,非常感谢!如此简单,错误消息却无济于事。
猜你喜欢
  • 2013-06-20
  • 2012-01-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-08-26
  • 1970-01-01
  • 2016-01-22
  • 1970-01-01
相关资源
最近更新 更多