一个控件的多个dataContext-MVVM [英] Multiple dataContext for one control - MVVM

查看:261
本文介绍了一个控件的多个dataContext-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?

推荐答案

可以使用某些绑定属性为绑定指定与默认DataContext

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不需要指定Name.

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.

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

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