C# 如何在WPF MVVM中使用外键绑定组合框

C# 如何在WPF MVVM中使用外键绑定组合框,c#,wpf,silverlight,xaml,mvvm,C#,Wpf,Silverlight,Xaml,Mvvm,我知道有很多关于组合框数据绑定的问题,也有很多教程,但我觉得这些教程很难。所以,我要问这个问题 假设我的数据库中有两个表: 顾客 CustomerID Name GenderID 性别类型 GenderTypeID GenderType 我已经使用ADO.Net实体数据模型创建了我的模型。所以,我使用的是实体框架 现在我有了一个ViewModel,其中声明了一个名为Customers的属性,如下所示: private List<Customer> _customers; publ

我知道有很多关于组合框数据绑定的问题,也有很多教程,但我觉得这些教程很难。所以,我要问这个问题

假设我的数据库中有两个表:

顾客

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");
    }
}
private List\u客户;
公开名单客户
{
得到
{
退回客户;
}
设置
{
_顾客=价值;
不动产变更(“客户”);
}
}
现在我有这样一个观点:

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

..
..
..
..
我不知道如何绑定combobox,这样我就可以看到男性和女性作为combobox的项目,当我选择时,我应该得到相应的GenderID而不是GenderType

我知道这是一个非常简单和直截了当的问题,但我对WPF非常陌生,正在努力学习它。

试试这个:

<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}" />

ItemsSource=“{Binding MyGenderTypes}”
DisplayMemberPath=“GenderType”
SelectedValuePath=“GenderID”
SelectedValue=“{Binding SelectedGenderID,Mode=TwoWay}”/

您将获得显示GenderType的组合框,但所选值将是相应的GenderID。如您所愿…

在ViewModel类中添加
性别

public List<GenderTypes> Genders
        {
            get
            {
                return _genders;
            }
            set
            {
                _genders = value;
                OnPropertyChanged("Genders");
            }
        }
公开列出性别
{
得到
{
返回性别;
}
设置
{
_性别=价值;
财产变更(“性别”);
}
}
像这样使用之后

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


窗口的数据上下文当前绑定到什么?它绑定到MainWindowViewModel。我已经更新了我的xaml代码。我认为
DisplayMemberPath
必须是属性的名称,而不是绑定,除非您计划动态提供属性的名称。请给出一些建议。与其使用
列表
,不如使用
可观察收集
。我已尝试应用您的解决方案。但我在组合框项目中没有得到任何东西。因此,我检查了输出窗口以查找绑定错误,在那里我得到了以下错误:
ObjectContext实例已被释放,无法再用于需要连接的操作。
。当将
实体对象分配给
项源时,请使用
ToList()
。例如
listBox.ItemsSource=db.Customers.ToList()
etc但我不想像上面的例子那样使用listBox。我只想让我的组合框在网格中。但Grid没有任何ItemsSource属性。我已尝试实现您的xaml,但在combobox中没有任何内容。代码预期MyGenderTypes为List或ObservableCollection,应该首先填充它,以便您可以在combobox中看到它们的显示。你做到了吗?我测试了代码,组合框按预期显示男性和女性。我用两种性别类型填充MyGenderTypes,一种是GenderTypeID=0,另一种是GenderTypeID=1