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

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

问题描述

我已经创建,将参照这个问题的文件是:



  TechnicainSelectionView.xaml 
TechnicianSelectionView的.cs
TechnicianSelectionViewModel.cs
Technician.cs(代码第一个实体)

我有以下XAML在我TechnicanSelectionView.xaml

 <用户控件的xmlns等等......这里的
D:DesignHeight =48D:DesignWidth =300>
<电网>
< StackPanel的>
<标签内容=选择一个技术员来运行测试粗细=大胆>< /标签>
<组合框的ItemsSource ={结合技术员,模式=双向}>< /组合框>
< / StackPanel的>
< /网格>
< /用户控件>

技术员到的属性的ItemSource设置绑定到状态,它无法解析技术人员由于未知的DataContext。



所以,如果我们看一下我的TechnicianSelectionView的.cs代码隐藏...

 公共部分类TechnicianSelectionView:用户控件
{
公共TechnicianSelectionViewModel视图模型{搞定;私人集; }

公共TechnicianSelectionView()
{
的InitializeComponent();

Technician.GenerateSeedData();

视图模型=新TechnicianSelectionViewModel();
的DataContext =视图模型;
}
}



...我们看到,我设置视图的的DataContext我TechnicianSelectionViewModel ...

 公共类TechnicianSelectionViewModel:ViewModelBase 
{
公众的ObservableCollection<技师>技术员{搞定;组; }

公共TechnicianSelectionViewModel()
{
技术员=新的ObservableCollection<技师>();
}

公共BOOL IsLoaded {搞定;私人集; }

公共无效LoadTechnicians()
{
名单,LT;技师>技术人员;

使用(VAR DB =新的TestContext())
{
VAR的查询=从db.Technicians
选择高科技技术;

的foreach(查询VAR技术人员)
{
Technicians.Add(技师);
}
}

IsLoaded = TRUE;
}
}



Techicians是我的ViewModel ... <属性/ p>

所以,在已经设置的DataContext的观点,为什么不能解析的视图模型,因为它是要绑定到DataContext /物业技术人员?



修改



按照下面的注释关注。这是设计的时间问题,而不是编译时间。我应该在一开始已经表明这一点。


解决方案

您需要指定在XAML数据上下文的类型来获得设计 - 时间支持。即使你在指定的数据上下文代码隐藏,设计师是不会承认这一点。



尝试把你的XAML以下内容:

  D:的DataContext ={D:DesignInstance VM:TechnicianSelectionViewModel}

请参阅的this链接了解更多详情。


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)

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>

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

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;
    }
}

... 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;
    }
}

Techicians is a property on my ViewModel...

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?

EDIT:

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.

解决方案

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.

Try putting the following in your xaml:

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

See this link for more details.

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

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