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

查看:107
本文介绍了需要进一步限定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,无法解析技术人员。

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 / property?

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天全站免登陆