【问题标题】:How to bind a combobox with Foreign Key in WPF MVVM如何在 WPF MVVM 中使用外键绑定组合框
【发布时间】: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


【解决方案1】:

试试这个:

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

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

【讨论】:

  • 我已尝试实现您的 xaml,但在组合框中我没有得到任何东西。
  • 代码期望 MyGenderTypes 为 List 或 ObservableCollection,并且应该首先填充它,以便您可以看到它们显示在组合框中。你做到了吗?
  • 我测试了代码,并且组合框按预期显示了男性和女性。我用 2 个 GenderTypes 填充了 MyGenderTypes,一个 GenderType = "Male" GenderTypeID=0,另一个 GenderType="Female" GenderTypeID=1
【解决方案2】:

在 ViewModel 类中添加Genders

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>

【讨论】:

  • 我认为 DisplayMemberPath 需要是属性的名称而不是绑定,除非您打算动态提供属性的名称。
  • 只是一些建议。而不是List,更好的方法是使用ObservableCollection。
  • 我已尝试应用您的解决方案。但我在组合框项目中没有得到任何东西。所以我检查了输出窗口以找到绑定错误,我得到了这个错误:The ObjectContext instance has been disposed and can no longer be used for operations that require a connection.
  • 将Entity Objects 分配给Itemssource 时,请使用ToList()。例如listBox.ItemsSource=db.Customers.ToList()等
  • 但是我不想像你在上面的例子中那样使用列表框。我只想在网格中有我的组合框。但是 Grid 没有任何 ItemsSource 属性。
猜你喜欢
  • 2015-09-15
  • 1970-01-01
  • 2021-10-11
  • 2015-07-11
  • 2015-04-27
  • 2023-03-18
  • 2013-06-20
  • 2012-08-20
  • 2011-06-13
相关资源
最近更新 更多