一个控件的多个dataContext - MVVM

时间:2013-03-19 11:58:14

标签: wpf mvvm datacontext

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

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

<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显示一些超链接(&#34; Data.MapLink&#34;是包含链接值的对象),每次点击此链接都将触发命令&#34; OpenLinkCommand&#34;

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

  1. 保留超链接dataContext - 命令不起作用,超链接将获取Data.MapLink值。

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

  3. 遗憾的是,我没有选择将这些项目放在相同的dataContext中,所以我必须找到一种方法告诉命令它dataContext是&#34; X&#34;告诉hyperLink它dataContext是&#34; Y&#34;。

    我希望我的问题很明确 我该如何解决这个问题?

3 个答案:

答案 0 :(得分:16)

您可以使用一些绑定属性为绑定指定不同的Source,而不是默认DataContext

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

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

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

您还可以在绑定中使用RelativeSource来查找指定对象类型的VisualTree中的对象,并将其用作绑定源。此示例与上面的示例完全相同,只是它使用RelativeSource而不是ElementName,因此您的GridView不需要指定Name

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

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

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

基于your comment here关于绑定到单身人士的问题,这可能是您的最佳选择。

答案 1 :(得分:0)

您必须拥有所需数据上下文的实例(通常在控件或窗口的资源中)。完成后,您应该能够显式设置文本块的数据上下文,而不是自动继承父数据上下文。

例如:

<TextBlock DataContext="{StaticResource MyDataMapLinkDataContext}" Text="{Binding Data.MapLink}" TextDecorations="underline"/>

答案 2 :(得分:0)

如果您确实需要使用其他属性来获取额外的数据上下文,那么您可以使用附加属性。

<强> XAML     

    <Window.Resources>
        <Style TargetType="Button">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="Button">
                        <ContentPresenter Content="{Binding (local:ExtraDataContextProvider.ExtraDataContext), RelativeSource={RelativeSource TemplatedParent}}"/>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </Window.Resources>

    <Grid>
        <Button Margin="172,122,131,79" Foreground="Green" local:ExtraDataContextProvider.ExtraDataContext="A test">
            test
        </Button>
    </Grid>
</Window>

<强>代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WpfApplication1
{

    public class ExtraDataContextProvider : DependencyObject
    {
        public static object GetExtraDataContext(DependencyObject obj)
        {
            return (object)obj.GetValue(ExtraDataContextProperty);
        }

        public static void SetExtraDataContext(DependencyObject obj, object value)
        {
            obj.SetValue(ExtraDataContextProperty, value);
        }

        public static readonly DependencyProperty ExtraDataContextProperty = DependencyProperty.RegisterAttached("ExtraDataContext", typeof(object), typeof(ExtraDataContextProvider), new PropertyMetadata(null));
    }

    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
    }
}