[英]How to bind a combobox with Foreign Key in WPF MVVM
I know that there are many questions regarding DataBinding a combobox and also there are many tutorials but I feel those tutorials hard. 我知道关于DataBinding组合框有很多问题,也有很多教程,但是我觉得这些教程很难。 So, I am asking this question.
所以,我在问这个问题。
Suppose I have two tables in my database: 假设我的数据库中有两个表:
Customer 顾客
CustomerID
Name
GenderID
GenderTypes GenderTypes
GenderTypeID
GenderType
I have created my models using ADO.Net Entity Data Model. 我已经使用ADO.Net实体数据模型创建了模型。 So, I am using Entity Framework.
因此,我正在使用实体框架。
Now I have a ViewModel in which I declare a property called Customers as below: 现在,我有一个ViewModel,其中声明了一个名为Customer的属性,如下所示:
private List<Customer> _customers;
public List<Customer> Customers
{
get
{
return _customers;
}
set
{
_customers = value;
OnPropertyChanged("Customers");
}
}
Now I have a View Like this: 现在我有一个像这样的视图:
<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>
I dont know how to bind the combobox, so that I can see Male and Female as the items of combobox and when I select I should get corresponding GenderID instead of GenderType. 我不知道如何绑定组合框,因此我可以将Male和Female视为组合框的项目,并且当我选择时,我应该获取对应的GenderID而不是GenderType。
I know this is a very simple and straight forward question but I am very much new to WPF and trying to learn it. 我知道这是一个非常简单直接的问题,但是对于WPF并尝试学习它来说我是一个新手。
Try this: 尝试这个:
<ComboBox
<!--ItemsSource bound to property of type collection of GenderTypes, containing all gender types you have -->
ItemsSource="{Binding MyGenderTypes}"
<!--Tell comboBox to display GenderType property of selected GenderTypes-->
DisplayMemberPath="GenderType"
<!--Tell comboBox that the SelectedValue should be GenderID property of selected GenderTypes-->
SelectedValuePath="GenderID"
<!--SelectedValue bound to property of type int (the same type of GenderID)-->
SelectedValue="{Binding SelectedGenderID, Mode=TwoWay}" />
You will get the combobox displaying GenderType, but the selected value will be the corresponding GenderID. 您将获得显示GenderType的组合框,但是所选值将是相应的GenderID。 Just as you wish...
如你所愿...
Add Genders
in ViewModel class 在ViewModel类中添加
Genders
public List<GenderTypes> Genders
{
get
{
return _genders;
}
set
{
_genders = value;
OnPropertyChanged("Genders");
}
}
After use like this. 这样使用后。
<ListBox ItemsSource="{Binding Customers}">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel>
<TextBlock Text="Name" />
<TextBox Text="{Binding Name}" />
<TextBlock Text="Gender" />
<ComboBox ItemsSource="{Binding Genders,RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type Window}}}" DisplayMemberPath="GenderType" SelectedValue="{Binding GenderID}" SelectedValuePath="GenderTypeID"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.