一个控件的多个数据上下文 - MVVM [英] Multiple dataContext for one control - MVVM

查看:15
本文介绍了一个控件的多个数据上下文 - MVVM的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不确定我的问题标题是否完全代表我的问题,我会尽力解释:

I am not sure if my question header represent exactly my problem, I will do the best to explain:

我有一个网格单元 DataTemplate:(网格属于第三方公司,但对我的问题并不重要)

I have a grid cell DataTemplate: (the grid belong to third party company but it`s not important for my question)

<DataTemplate>
    <TextBlock>
        <Hyperlink Command="{Binding OpenLinkCommand}"> 
            <Hyperlink.ToolTip>
                <TextBlock Text="{Binding Data.MapLink}"/>
            </Hyperlink.ToolTip>
            <TextBlock Text="{Binding Data.MapLink}" TextDecorations="underline">
        </Hyperlink>
    </TextBlock>
</DataTemplate>

我想让这个 DataTemplate 显示一些超链接(Data.MapLink"是包含链接值的对象),每次点击这个链接都会触发命令OpenLinkCommand".

I want make this DataTemplate to show some hyperlink ("Data.MapLink" is the object which contain the link value) and each click on this link will fire the command "OpenLinkCommand".

问题是Data.MapLink"和OpenLinkCommand"位于不同的dataContext中,然后我必须选择下一个选项之一:

The problem is that "Data.MapLink" and "OpenLinkCommand" are located in different dataContext and then I have to choose one of the next choices:

  1. 将超链接 dataContext 保持原样 - 命令将不起作用,超链接将获得 Data.MapLink 值.

  1. leave hyperlink dataContext as it - the command won`t work and the hyperlink will get the Data.MapLink value.

将超链接 dataContext 更改为命令 datacontext - 命令将起作用,但超链接名称将为空.

change hyperlink dataContext to the command datacontext - The command will work but the hyperlink name will be empty.

遗憾的是,我没有选择将这些项目放在同一个 dataContext 中,所以我必须找到一种方法来告诉命令它的 dataContext 是X"并告诉超链接它的 dataContext 是Y".

Regretfully I don`t have option put those items in same dataContext so I must find a way how to tell the command that it dataContext is "X" and tell the hyperLink that it dataContext is "Y".

我希望我的问题很清楚我该如何解决这个问题?

I am hoping that my question is clear How can I solve this problem?

推荐答案

您可以使用一些绑定属性为您的绑定指定与默认 DataContextSource>

There are some binding properties you can use to specify a different Source for your binding than the default DataContext

最常见的是 ElementNameRelativeSource,它们会在 VisualTree 中找到另一个 UI 元素,以便您可以绑定到它的属性.

The most common ones are ElementName or RelativeSource, which will find another UI element in the VisualTree so you can bind to it's properties.

例如,下面使用ElementName告诉绑定它应该使用MyGridView作为绑定源,并绑定到MyGridView.DataContext.OpenLinkCommand

For example, the following uses ElementName to tell the binding that it should use MyGridView as the binding source, and to bind to MyGridView.DataContext.OpenLinkCommand

<Hyperlink Command="{Binding ElementName=MyGridView, 
                             Path=DataContext.OpenLinkCommand}"> 

您还可以在绑定中使用 RelativeSource 在指定对象类型的 VisualTree 上进一步查找对象,并将其用作绑定源.这个例子和上面的例子做同样的事情,除了它使用 RelativeSource 而不是 ElementName,所以你的 GridView 不需要有名称 指定.

You can also use RelativeSource in a binding to find an object further up the VisualTree of the specified object type, and use it as the binding source. This example does the same thing as the above example, except it uses RelativeSource instead of ElementName, so your GridView doesn't need to have a Name specified.

<Hyperlink Command="{Binding 
               RelativeSource={RelativeSource AncestorType={x:Type GridView}}, 
               Path=DataContext.OpenLinkCommand}"> 

第三个选项是将绑定的 Source 属性设置为静态对象,如下所示:

A third option is to set the binding's Source property to a static object, like this:

<Hyperlink Command="{Binding 
               Source={x:Static local:MyStaticClass.OpenLinkCommand}}"> 

基于您在此处的评论关于绑定到单身人士,这可能是您的最佳选择.

Based on your comment here about binding to a singleton, this would probably be the best option for you.

这篇关于一个控件的多个数据上下文 - MVVM的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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