【发布时间】:2012-12-28 12:22:25
【问题描述】:
没问题
我创建了一个依赖属性,它接受一个字符串值。我将它设置在 TextBlock 上,它可以工作:
<TextBlock dp:ElementDataContext.ElementName="LvMain">
我验证了属性ElementDataContext.ElementName 设置为“LvMain”。
问题
现在问题来了:在TextBlock 的上下文菜单中,我想通过PlacementTarget 绑定到这个依赖属性。
这是我尝试的方法。这是我的 XAML 的摘录,其中包含 TextBlock 和 ContextMenu:
<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.ElementNamePlacementTarget.ElementDataContext.ElementName
没有任何作用。什么是正确的语法?这甚至可能吗?
【问题讨论】:
-
看看这个:wpftutorial.net/DebugDataBinding.html 可能会帮助您找到有关错误的更多信息
标签: wpf data-binding dependency-properties