我需要什么来进一步限定 DataContext 的绑定? [英] What do I need to further qualify the DataContext for a binding?

查看:18
本文介绍了我需要什么来进一步限定 DataContext 的绑定?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已创建并将在此问题中引用的文件是:

The files I have created and will be referring to in this question are:

TechnicainSelectionView.xaml
TechnicianSelectionView.cs
TechnicianSelectionViewModel.cs
Technician.cs (Code First Entity)

我的 TechnicanSelectionView.xaml 中有以下 xaml

I have the following xaml in my TechnicanSelectionView.xaml

<UserControl xmlns etc... here" 
             d:DesignHeight="48" d:DesignWidth="300">
    <Grid>
        <StackPanel>
            <Label Content="Select a Technican to run the test" FontWeight="Bold"></Label>
            <ComboBox ItemsSource="{Binding Technicians, Mode=TwoWay}"></ComboBox>
        </StackPanel>
    </Grid>
</UserControl>

ItemSource 设置为绑定到的 Technicians 属性声明它由于未知的 DataContext 无法解析 Technicians.

The Technicians property to which the ItemSource is set to bind to states that it Cannot resolve Technicians due to an unknown DataContext.

因此,如果我们查看我的 TechnicianSelectionView.cs 代码隐藏...

So if we look to my TechnicianSelectionView.cs code-behind...

public partial class TechnicianSelectionView : UserControl
{
    public TechnicianSelectionViewModel ViewModel { get; private set; }

    public TechnicianSelectionView()
    {
        InitializeComponent();

        Technician.GenerateSeedData();

        ViewModel = new TechnicianSelectionViewModel();
        DataContext = ViewModel;
    }
}

...我们看到我正在将视图的 DataContext 设置为我的 TechnicianSelectionViewModel ...

... we see that I am setting the view's DataContext to my TechnicianSelectionViewModel ...

public class TechnicianSelectionViewModel : ViewModelBase
{
    public ObservableCollection<Technician> Technicians { get; set; }

    public TechnicianSelectionViewModel()
    {
        Technicians = new ObservableCollection<Technician>();
    }

    public bool IsLoaded { get; private set; }

    public void LoadTechnicians()
    {
        List<Technician> technicians;

        using (var db = new TestContext())
        {
            var query = from tech in db.Technicians
                        select tech;

            foreach (var technician in query)
            {
                Technicians.Add(technician);
            }
        }

        IsLoaded = true;
    }
}

技术人员是我的 ViewModel 的一个属性...

Techicians is a property on my ViewModel...

既然已经为视图设置了 DataContext,为什么不能将 ViewModel 上的 Technicians 解析为它要绑定到的 DataContext/属性?

So having already set the DataContext for the view, why can't it resolve Technicians on the ViewModel as the DataContext/property it is going to bind to?

根据下面评论中的一个问题.这是设计时问题,而不是编译时问题.我应该在一开始就指出这一点.

As per a concern in a comment below. This is a design time problem and not compile time. I should have indicated this at the start.

推荐答案

您需要在 xaml 中指定数据上下文的类型以获得设计时支持.即使您在代码隐藏中分配了数据上下文,设计人员也不会意识到这一点.

You need to specify the type of data context in the xaml to get design-time support. Even though you assigned the data context in code-behind, the designer is not going to recognize that.

尝试将以下内容放入您的 xaml:

Try putting the following in your xaml:

d:DataContext="{d:DesignInstance vm:TechnicianSelectionViewModel}"

请参阅此链接更多详情.

这篇关于我需要什么来进一步限定 DataContext 的绑定?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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