wpf 我需要什么来进一步限定 DataContext 的绑定?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15101696/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What do I need to further qualify the DataContext for a binding?
提问by Isaiah Nelson
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
我的 TechnicanSelectionView.xaml 中有以下 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.
ItemSource 设置为绑定到的 Technicians 属性声明它 Cannot resolve Technicians due to an unknown DataContext.
So if we look to my TechnicianSelectionView.cs code-behind...
因此,如果我们查看我的 TechnicianSelectionView.cs 代码隐藏...
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 ...
...我们看到我正在将视图的 DataContext 设置为我的 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...
技术人员是我的 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?
因此,已经为视图设置了 DataContext,为什么它不能将 ViewModel 上的技术人员解析为它要绑定到的 DataContext/属性?
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.
根据下面评论中的一个问题。这是设计时问题,而不是编译时问题。我应该在一开始就指出这一点。
回答by Brandon Baker
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:
尝试将以下内容放入您的 xaml:
d:DataContext="{d:DesignInstance vm:TechnicianSelectionViewModel}"
See this linkfor more details.
有关更多详细信息,请参阅此链接。
回答by Aboo
In my Xamarin Forms Xaml file I used the following lines in the header (ContentPage tag) and it worked perfectly as I wanted.
在我的 Xamarin Forms Xaml 文件中,我在标题(ContentPage 标记)中使用了以下几行,它按我的需要完美运行。
Basically now
现在基本上
- the intellisense shows the fields in the binding
my Resharper is able to rename the binding in the Xaml file if I refactor the name of the property
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:vm="clr-namespace:YourApplicationName.ViewModels;assembly=YourApplicationName" mc:Ignorable="d" d:DataContext="{d:DesignInstance {x:Type vm:CurrentPageViewModel}}"
- 智能感知显示绑定中的字段
如果我重构属性的名称,我的 Resharper 能够重命名 Xaml 文件中的绑定
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:vm="clr-namespace:YourApplicationName.ViewModels;assembly=YourApplicationName" mc:Ignorable="d" d:DataContext="{d:DesignInstance {x:Type vm:CurrentPageViewModel}}"

