使用 WPF 和 Entity Framework 为查找组合框设置绑定

Setting binding for lookup combobox using WPF and Entity Framework

我正在尝试使用 C#、WPF 和 Entity Framework(数据库优先)创建处理数据库数据的应用程序。为了学习这些方法,我创建了一个小的示例应用程序,它使用 SQL 服务器和 Northwind 数据库作为后端(参见这张图片中的模型 tinypic.com/r/1zl6d0x/9)。

我使用数据网格创建了用于查看订单和订单详细信息的简单表单。现在我想使用组合框来为订单挑选客户,但我在为组合框设置查找时遇到了问题。现在客户组合框显示错误的值,而且如果我更改其中的值,所选值将更改为所有行(在此图片 tinypic.com/r/2mnejac/9 中看到)。

目前 window 的主要代码是这样的:

using System.Windows;
using System.Windows.Data;
using System.Data.Entity; 

namespace NorthTest
{
    public partial class MainWindow : Window
    {
        NorthwindConnection context;
        CollectionViewSource ordersViewSource;
        CollectionViewSource customersViewSource;

        public MainWindow()
        {
            InitializeComponent();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            context = new NorthwindConnection();
            context.Orders.Load();
            context.Customers.Load();

            ordersViewSource = ((CollectionViewSource)(this.FindResource("ordersViewSource")));
            ordersViewSource.Source = context.Orders.Local;

            customersViewSource = ((System.Windows.Data.CollectionViewSource)(this.FindResource("customersViewSource")));
            customersViewSource.Source = context.Customers.Local;
        }
    }
}

和XAML:

<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:NorthTest"
        mc:Ignorable="d"
        x:Class="NorthTest.MainWindow"
        Loaded="Window_Loaded">
    <Window.Resources>
        <CollectionViewSource x:Key="ordersViewSource" />
        <CollectionViewSource x:Key="customersViewSource" />
        <CollectionViewSource x:Key="ordersOrder_DetailsViewSource"
                              Source="{Binding Order_Details, Source={StaticResource ordersViewSource}}" />
    </Window.Resources>
    <Grid DataContext="{StaticResource ordersViewSource}">
        <Grid.RowDefinitions>
            <RowDefinition Height="214*" />
            <RowDefinition Height="291*" />
        </Grid.RowDefinitions>
        <DataGrid x:Name="ordersDataGrid"
                  AutoGenerateColumns="False"
                  EnableRowVirtualization="True"
                  ItemsSource="{Binding}"
                  RowDetailsVisibilityMode="VisibleWhenSelected">
            <DataGrid.Columns>
                <DataGridTemplateColumn x:Name="orderIDColumn"
                                        Header="Order ID"
                                        Width="SizeToHeader">
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <Label Content="{Binding OrderID}" />
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>
                <DataGridTemplateColumn x:Name="customerIDColumn"
                                        Header="Customer ID"
                                        Width="SizeToHeader">
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <ComboBox ItemsSource="{Binding Source={StaticResource customersViewSource}}"
                                      DisplayMemberPath="CustomerID"
                                      SelectedItem="{Binding Path=Customers}"/>
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>
                <DataGridTemplateColumn x:Name="customerIDColumn2"
                                        Header="Customer ID2"
                                        Width="SizeToHeader">
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <Label Content="{Binding CustomerID}" />
                       </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>
            </DataGrid.Columns>
        </DataGrid>

        <DataGrid x:Name="order_DetailsDataGrid"
                  AutoGenerateColumns="False"
                  EnableRowVirtualization="True"
                  ItemsSource="{Binding Source={StaticResource ordersOrder_DetailsViewSource}}"
                  Grid.Row="1"
                  RowDetailsVisibilityMode="VisibleWhenSelected">
            <DataGrid.Columns>
                <DataGridTextColumn x:Name="discountColumn"
                                    Binding="{Binding Discount}"
                                    Header="Discount"
                                    Width="SizeToHeader" />
                <DataGridTemplateColumn x:Name="productIDColumn"
                                        Header="Product ID"
                                        Width="SizeToHeader">
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <ComboBox>
                                <ComboBoxItem Content="{Binding ProductID}" />
                            </ComboBox>
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>
                <DataGridTextColumn x:Name="quantityColumn"
                                    Binding="{Binding Quantity}"
                                    Header="Quantity"
                                    Width="SizeToHeader" />
                <DataGridTextColumn x:Name="unitPriceColumn"
                                    Binding="{Binding UnitPrice}"
                                    Header="Unit Price"
                                    Width="SizeToHeader" />
            </DataGrid.Columns>
        </DataGrid>     
    </Grid>
</Window>

我应该如何为 combobox/lookup table 设置绑定和视图源以便组合框显示正确的客户?

我用谷歌搜索了一下,例如。 here and here 但我就是不明白(也许我太笨了)。

(...) if I change value in it, selected value is changed to all rows

要解决这个问题,我会添加到组合框:

IsSynchronizedWithCurrentItem="False"