来自 ToolTip 或 ContextMenu 的 RelativeSource 绑定 [英] RelativeSource binding from a ToolTip or ContextMenu

查看:22
本文介绍了来自 ToolTip 或 ContextMenu 的 RelativeSource 绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这里做错了什么?:

What am I doing wrong here?:

 <GridViewColumn>
    <GridViewColumn.CellTemplate>
       <DataTemplate>
          <Button>
            <Button.ToolTip>
              <TextBlock Text="{Binding Path=Title, RelativeSource={RelativeSource AncestorType=Window}}" />

那只是一个简化的例子,无论如何都行不通:)实际上,我需要从 Window 的 DataContext 范围内的另一个属性中获取一个值.

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.

请帮帮我.

推荐答案

这很棘手,因为 ToolTip 不是 VisualTree 的一部分.在这里你会看到一个很酷的解决方案ContextMenus 也有同样的问题.与获取工具提示的方法相同.

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.

更新
遗憾的是,该链接已消失,我再也找不到引用的文章了.
据我所知,引用的博客已经展示了如何绑定到另一个 VisualTree 的 DataContext,这在从 ToolTip、ContextMenu 或 Popup 绑定时通常是必需的.

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.

一个很好的方法是在 PlacementTarget 的标签属性中提供所需的实例(例如 ViewModel).以下示例为访问 ViewModel 的命令实例执行此操作:

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:

<Button Tag="{Binding DataContext,RelativeSource={RelativeSource Mode=Self}}">
  <Button.ContextMenu>
    <ContextMenu>
       <MenuItem Command="{Binding PlacementTarget.Tag.DesiredCommand,RelativeSource={RelativeSource Mode=FindAncestor,AncestorType=ContextMenu}}" .../>
    <ContextMenu>
  </Button.ContextMenu>
</Button>

我没有测试过它,我上次这样做已经很长时间了.如果它不适合您,请发表评论.

更新 2

由于写这个答案的原始链接不见了,我点击了archive.org和找到原始博客条目.这是博客的逐字逐句:

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:

因为 WPF 中的 ContextMenu 不存在于您的页面/窗口/控件本身,数据绑定可能有点棘手.我在网上到处搜索这个,最常见的答案似乎是只在后面的代码中做".错误的!一世并没有进入 XAML 的美妙世界在后面的代码中做事.

Because a ContextMenu in WPF does not exist within the visual tree of your page/window/control per se, data binding can be a little tricky. I have searched high and low across the web for this, and the most common answer seems to be "just do it in the code behind". WRONG! I didn’t come in to the wonderful world of XAML to be going back to doing things in the code behind.

这是我的示例,它将允许您绑定到一个字符串作为窗口的属性存在.

Here is my example to that will allow you to bind to a string that exists as a property of your window.

public partial class Window1 : Window
{
    public Window1()
    {
        MyString = "Here is my string";
    }

    public string MyString
    {
        get;
        set;

    }
}


<Button Content="Test Button" 
     Tag="{Binding RelativeSource={RelativeSource AncestorType={x:Type Window}}}">
  <Button.ContextMenu>
    <ContextMenu DataContext="{Binding Path=PlacementTarget.Tag, 
          RelativeSource={RelativeSource Self}}" >
      <MenuItem Header="{Binding MyString}"/>
    </ContextMenu>
  </Button.ContextMenu>   
</Button>

重要的部分是按钮上的标签(虽然你可以像轻松设置按钮的 DataContext).这存储了对父窗口.ContextMenu 能够访问这个通过它的 PlacementTarget 属性.然后你可以传递这个上下文向下浏览您的菜单项.

The important part is the Tag on the button(although you could just as easily set the DataContext of the button). This stores a reference to the parent window. The ContextMenu is capable of accessing this through it’s PlacementTarget property. You can then pass this context down through your menu items.

我承认这不是世界上最优雅的解决方案.但是,它胜过在后面的代码中设置内容.如果有人有更好的方式来做到这一点,我很想听听.

I’ll admit this is not the most elegant solution in the world. However, it beats setting stuff in the code behind. If anyone has an even better way to do this I’d love to hear it.

这篇关于来自 ToolTip 或 ContextMenu 的 RelativeSource 绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆