来自工具提示或上下文菜单的相对源绑定
我在这里做错了什么?:
<GridViewColumn>
<GridViewColumn.CellTemplate>
<DataTemplate>
<Button>
<Button.ToolTip>
<TextBlock Text="{Binding Path=Title, RelativeSource={RelativeSource AncestorType=Window}}" />
这只是一个简化的例子,无论如何都不起作用:) 实际上,我需要从窗口 DataContext 范围内的另一个属性获取值。
请帮助我。
What am I doing wrong here?:
<GridViewColumn>
<GridViewColumn.CellTemplate>
<DataTemplate>
<Button>
<Button.ToolTip>
<TextBlock Text="{Binding Path=Title, RelativeSource={RelativeSource AncestorType=Window}}" />
That's just a simplified example, that doesn't work anyway :)
Actually I need to get a value from another property that is in scope of the Window's DataContext.
Help me pls.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这很棘手,因为工具提示不是 VisualTree 的一部分。 在这里您可以看到一个很酷的解决方案ContextMenus 也有同样的问题。您可以采用相同的方式获取工具提示。
更新
遗憾的是,链接已消失,我再也找不到引用的文章了。
据我记得,引用的博客展示了如何绑定到另一个 VisualTree 的 DataContext,这在从 ToolTip、ContextMenu 或 Popup 绑定时通常是必需的。
一个很好的方法是在 PlacementTarget 的 Tag 属性中提供所需的实例(例如 ViewModel)。下面的示例执行此操作以访问 ViewModel 的命令实例:
我还没有测试过它,而且我上次这样做已经很长时间了。如果它不适合您,请发表评论。
更新 2
由于撰写此答案的原始链接已消失,我点击 archive.org 和 找到原始博客条目。这是博客中的逐字记录:
This is tricky because ToolTip is not part of the VisualTree. Here you see a cool solution for the same problem with ContextMenus. The same way you can go for the ToolTip.
UPDATE
Sadly the link is gone and I have not found the referenced article anymore.
As far as I remember, the referenced blog has shown how to bind to a DataContext of another VisualTree, which is often necessay when binding from a ToolTip, a ContextMenu or a Popup.
A nice way to do this, is to provide the desired instance (e.g. ViewModel) within the Tag-property of the PlacementTarget. The following example does this for accessing a Command-instance of a ViewModel:
I have not tested it and its a long time I did this the last time. Please make a comment if it does not work for you.
UPDATE 2
As the original link that this answer was written about is gone, I hit archive.org and found the original blog entry. Here it is, verbatim from the blog:
如下:
PlacementTarget 是拥有 ContextMenu 的控件(例如:DataGrid)。不需要“标签”属性。
IsEnabled 绑定到 DataGrid 的“myProperty”值。
我测试了这个并且它有效。绑定方面也有类似的问题。
Per below:
PlacementTarget is the control that owns the ContextMenu (ex: DataGrid). No need for a "tag" property.
IsEnabled binds to the DataGrid's "myProperty" value.
I tested this and it works. Was having similar issue with binding.
由于 ContextMenu 不在可视化树中,因此绑定将不起作用。
一个简单的解决方案是使用代理模式,您可以创建一个继承自
DependencyObject
的包装类,并具有一个DependencyProperty
,它将保留您的DataContext
Window
,那么你就可以在XAML中拥有代理的资源,最后通过代理对象将你的MenuItem
命令绑定到你想要的命令。示例代理:
如何在 XAML 中使用:
发生了什么?
ProxyClass
的Data
属性将绑定到Window
的DataContext
,然后它拥有您的所有命令和属性ProxyClass
资源内的ViewModel
。这种方法的另一个好处是可移植性以及在多个视图和项目中的重用。
Because
ContextMenu
is not in visual tree, binding will not work.a simple solution is using Proxy Pattern, you can create a wrapper class that inherits from
DependencyObject
and has aDependencyProperty
that will keep aDataContext
of yourWindow
, then you can have a resource of the proxy in XAML and finally bind yourMenuItem
command to your desired command via the proxy object.Sample Proxy:
How to use in XAML:
What is happening?
Data
property ofProxyClass
will bind toDataContext
ofWindow
, then it has all of your comamnds and properties of yourViewModel
inside theProxyClass
resource.another benefit of this approach is portability and re-using in multiple views and projects.
我认为应该这样做:
I think it should be done like this: