2016-03-28 14 views
0

MVVM Light WPFアプリケーションをビルドするために、Visual Studio 2015でEntity Framework 6を​​使用しています。ナビゲーションプロパティICollectionをいくつかのCheckBoxコントロールにバインドする必要があります。従業員は0〜最大で3つのEmployeeStatusエンティティを持つことができます。従業員IDはEmployeeStatusエンティティのキ​​ーとして機能します。 EmployeeStatusは次に、employeeStatusIDのEmployeeStatusDescriptionテーブルに外部キーを持ちます。 EmployeeStatusDescriptionはステータスコードの説明を提供します(例えば、 "Archived"、 "Inactive"、 "Leave of Absence")。各EmployeeStatusは1つのEmployeeStatusDescriptionに対応します。Entity Frameworkのナビゲーションのプロパティを更新するICollectionの項目

EmployeeStatusは、Employeeクラスではこのように定義されて

:としてEmployeeStatusクラスで定義されている

public virtual ICollection<EmployeeStatu> EmployeeStatus { get; set; } 

EmployeeStatusDescription:私は3つのCheckBoxコントロールを表示し、各バインドしたい

public virtual EmployeeStatusDescription EmployeeStatusDescription { get; set; } 

EmployeeStatus ICollection値からの値。たとえば、従業員がステータスが「非アクティブ」でなく、ユーザーがそれを確認した場合、その従業員をEmployeeStatusコレクションに追加する必要があります。ユーザーがその項目のチェックを外した場合は、EmployeeStatusコレクションから削除するようにしたいと思います。

私はチェックボックスを保持するために次のStackPanelを作成しました。彼らは私のビューモデルのプロパティにバインドされている:

<StackPanel Grid.Row="12" 
      Grid.Column="1" 
      Orientation="Vertical"> 
    <StackPanel Orientation="Horizontal"> 
     <TextBlock HorizontalAlignment="Left" 
        VerticalAlignment="Top" 
        Text="Inactive" /> 
     <CheckBox IsChecked="{Binding IsSelectedEmployeeInActive}" /> 
    </StackPanel> 
    <StackPanel Orientation="Horizontal"> 
     <TextBlock HorizontalAlignment="Left" 
        VerticalAlignment="Top" 
        Text="Leave of Absence" /> 
     <CheckBox IsChecked="{Binding IsSelectedEmployeeLoa}" /> 
    </StackPanel> 
    <StackPanel Orientation="Horizontal"> 
     <TextBlock HorizontalAlignment="Left" 
        VerticalAlignment="Top" 
        Text="Archived" /> 
     <CheckBox IsChecked="{Binding IsSelectedEmployeeArchived}" /> 
    </StackPanel>          
</StackPanel> 

ここでチェックボックスのいずれかに結合した実施例のプロパティがIsChecked依存関係プロパティを制御しています:

private bool _isSelectedEmployeeInActive; 

public bool IsSelectedEmployeeInActive 
{ 
    get { return _isSelectedEmployeeInActive; } 
    set 
    { 
     if (_isSelectedEmployeeInActive == value) return; 

     _isSelectedEmployeeInActive = value; 
     RaisePropertyChanged(() => IsSelectedEmployeeInActive); 
    } 
} 

私が取得するために検索をやっていますエンティティコレクション:

var query = (from e in Context.Employees 
      .Include("EmployeeStatus.EmployeeStatusDescription") 
      .Where(comparison) 
      select e); 

SearchResults = new ObservableCollection<Employee>(query); 

答えて

1

あなたのプロパティの中に、私はコレクションにも追加/削除します。まだ選択していない場合は、選択した従業員をアクセスできるように保管します。

  if (_isSelectedEmployeeInActive == value) return; 

      _isSelectedEmployeeInActive = value; 

      //do updates to collection here 
      if (value) 
      { 
       SelectedEmployee.EmployeeStatus.Add(new EmployeeStatus("Inactive")); 
      } 
      else 
      { 
       SelectedEmployee.EmployeeStatus.Remove("Inactive")); 
      } 

      RaisePropertyChanged(() => IsSelectedEmployeeInActive); 

私はあなたの従業員の状況がどのように見えるかわからないが、あなたのアイデアを得ます。 変更がデータベースに保存されるように、コンテキストのSaveChangesを呼び出すボックスのチェックが完了したら、必ず確認してください。

+0

ありがとう、@ bme2010。すでに存在していないEmployeeStatusesのICollectionに値を追加しないようにするにはどうすればよいでしょうか?その値がすでに存在する場合、その '.Add()'にEFが吹き飛ばされますか? – Alex

+1

EFは、外部キー制約に違反する場合にのみ爆発します。同じ名前の複数の値を持つことができる場合は、それらを追加することができます。重複をチェックするには、LINQ '.Where(x => x.Name ==" Inactive ")を使用することができます。ICollectionのCount()' – Brandon

+0

もう一度@ bme2010に感謝します。 'Add()'を実行する前にチェックを行います。 – Alex

関連する問題