Ich versuche, Anwendung für die Verarbeitung von Datenbankdaten mit C#, WPF und Entity Framework (Datenbank zuerst) zu erstellen. Um diese Methoden zu lernen, habe ich eine kleine Beispielanwendung erstellt, die SQL Server und die Northwind
Datenbank als Backend verwendet (siehe Modell in diesem Bild tinypic.com/r/1zl6d0x/9).Festlegen der Bindung für Suchfeld-Combobox mit WPF und Entity Framework
Ich erstellte ein einfaches Formular zum Anzeigen von Bestellungen und Bestellung Details mit Datagrids. Jetzt möchte ich Combobox verwenden, um den Kunden für die Bestellung auszuwählen, aber ich habe Probleme beim Einrichten der Suche für die Combobox. Nun zeigt die Kunden-Combobox einen falschen Wert an und wenn ich den Wert ändere, wird der ausgewählte Wert in alle Zeilen geändert (siehe in diesem Bild tinypic.com/r/2mnejac/9).
Im Moment Code für das Hauptfenster ist dies:
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;
}
}
}
und 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>
Wie soll ich Bindungen und viewsources für Combobox/Lookup-Tabelle gesetzt, so dass Combobox richtigen Kunden zeigt ?
Ich habe gegoogelt und zB gesucht. here und here aber ich verstehe es einfach nicht (vielleicht bin ich einfach zu dumm).