2016-03-20 7 views
1

C#、WPF、Entity Framework(データベースを最初に使用)を使用してデータベースデータを処理するアプリケーションを作成しようとしています。これらの方法を学ぶために、SQL ServerとNorthwindデータベースをバックエンドとして使用する小さなサンプルアプリケーションを作成しました(この画像はtinypic.com/r/1zl6d0x/9のモデルを参照)。WPFとEntity Frameworkを使用してルックアップコンボボックスのバインディングを設定する

データグリッドを使用して注文や注文の詳細を表示するためのシンプルなフォームを作成しました。今私は注文のために顧客を選ぶためにコンボボックスを使用したいが、私はコンボボックスの検索をセットアップするのに問題がある。今度は顧客コンボボックスが間違った値を表示していて、その値を変更すると、選択した値がすべての行に変更されます(この写真のtinypic.com/r/2mnejac/9を参照)。

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:メインウィンドウの瞬間コードで

はこれです

コンボボックスは、右の顧客を示しているので、私はコンボボックス/ルックアップ・テーブルのバインディングとviewsourcesを設定する必要がありますどのように
<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> 

私はグーグルで見ました。 herehereしかし、私はちょうどそれを取得しない(おそらく私はあまりにも愚かです)。

答えて

0

(...)私はそれで値を変更した場合、選択された値が、私はコンボボックスに追加します

は、この問題を解決するには、すべての行に変更されます。

IsSynchronizedWithCurrentItem="False" 
関連する問題