【发布时间】:2013-12-28 21:13:09
【问题描述】:
我知道有很多关于 DataBinding 组合框的问题,也有很多教程,但我觉得这些教程很难。所以,我在问这个问题。
假设我的数据库中有两个表:
客户
CustomerID
Name
GenderID
性别类型
GenderTypeID
GenderType
我使用 ADO.Net 实体数据模型创建了我的模型。所以,我正在使用实体框架。
现在我有一个 ViewModel,我在其中声明了一个名为 Customers 的属性,如下所示:
private List<Customer> _customers;
public List<Customer> Customers
{
get
{
return _customers;
}
set
{
_customers = value;
OnPropertyChanged("Customers");
}
}
现在我有这样的视图:
<Window ......>
<Window.DataContext>
<vm:MainWindowViewModel />
</Window.DataContext>
<Grid>
<Grid.RowDefinitions>
..
..
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
..
..
</Grid.ColumnDefinitions>
<TextBlock Text="Name" ....../>
<TextBox Text="{Binding Name}"...../>
<TextBlock Text="Gender" ....../>
<TextBox ItemsSource="????"
SelectedValue="????"
SelectedValuePath="????"...../>
</Grid>
</Window>
我不知道如何绑定组合框,这样我就可以看到男性和女性作为组合框的项目,当我选择时,我应该得到对应的 GenderID 而不是 GenderType。
我知道这是一个非常简单直接的问题,但我对 WPF 非常陌生,并且正在努力学习它。
【问题讨论】:
-
当前绑定的窗口数据上下文是什么?
-
绑定到 MainWindowViewModel。我已经更新了有问题的 xaml 代码。
标签: c# wpf silverlight xaml mvvm