如何解决无法使用FindAncestor通过ContextMenu绑定到父属性
我有一个带有ContextMenu的ListBox,并且试图绑定到Tag。
我可以使用RelativeSource / FindAncestor从let data = UIImageJPEGRepresentation(image,1)!
request.send(withFormParameters: ["file":data],completionHandler: { (result,error) in
print(result)
print(error)
})
绑定到Tag
,但是这种方法对ItemTemplate
无效。
我在Visual Studio中浏览了Live Visual Tree,看到了ListBox项,但没有看到ContextMenu。如果ListBox不是可视树中ContextMenu的祖先,正确的绑定方法是什么?
注意:我打算在ContextMenu
中创建一个ContextMenu,以便可以在多个ListBox中使用,因此我不想使用Page.Resources
绑定到特定列表控件。
ElementName
解决方法
ContextMenu
与ListBox
不在同一视觉树中,因此RelativeSource
绑定不起作用。您可以改为:
<MenuItem Header="{Binding Path=PlacementTarget.Tag,RelativeSource={RelativeSource FindAncestor,AncestorType={x:Type ContextMenu}}}">
这是有效的,因为PlacementTarget
中的ContextMenu
是父ListBox
。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。