2012-10-12 3 views
4

이 포함 된 WPF 응용 프로그램 (Entity Framework 사용)에 DataGrid가 있습니다. 이 ComboBox은 드롭 다운에 표시되는 이름을 포함하는 테이블에 조인 된 참조를 사용하는 데이터 소스에 바인딩됩니다. 이 조인에 대해 ID 필드 (SalesActMgrID)를 사용하고 있습니다. 해당 테이블에서 특정 이름의 목록 <으로 드롭 다운 목록을 채 웁니다.WPF DataGrid 가져 오기 전에 값을 가져 오는 ComboBox

내 문제는 드롭 다운 목록에서 이름을 선택하면 SalesActMgrID을 선택한 이름으로 변경하는 대신 조인 된 테이블의 이름이 변경된다는 것입니다.

내 데이터 소스에서 ID를 업데이트하는 방법을 찾았지만 해당 이름에 올바른 ID를 얻을 수 있도록 드롭 다운에서 선택된 이름을 찾는 방법을 찾지 못했습니다. 데이터 격자가 EmployeeTime의 데이터 소스에 결합되어

   <DataGridComboBoxColumn 
        SelectedItemBinding="{Binding Path=ClientContract.StaffRole_SalesActMgr.StaffName}" 
        Header="Sales Act Mgr" 
        x:Name="salesActMgrColumn" Width="Auto" > 
       <DataGridComboBoxColumn.EditingElementStyle> 
        <Style TargetType="ComboBox"> 
         <Setter Property="ItemsSource" 
           Value="{Binding staffNamesListSAM}" /> 
         <Setter Property="IsReadOnly" 
           Value="True" /> 
        </Style> 
       </DataGridComboBoxColumn.EditingElementStyle> 
      </DataGridComboBoxColumn> 

:로

콤보 열이 정의되어있다. 전체가 테이블의 조인은 다음과 같습니다 : I 셀 편집하여 셀에 커밋을 수행하려면 다음 코드를 사용하고

EmployeeTime.ClientContractID is joined to ClientContract.ClientContractID {M-1} 
StaffRoles.StaffRoleID is joined to ClientContract.SalesActMgrID {1-M} 

.

private bool isManualEditCommit; 

    private void consultantsDataGrid_CellEditEnding(object sender, DataGridCellEditEndingEventArgs e) 
    { 
     if (!isManualEditCommit) 
     { 
      isManualEditCommit = true; 
      string head = e.Column.Header.ToString(); 
      bool doCommit = true; 

      switch (head) 
      { 
       case "Sales Act Mgr": 
        { 
         e.Cancel = true; 
         //This is where I have been able to 'hard code' in a different 
         //ID value into the SalesActMgrID field which then correctly 
         //updates the value, but I need to know what name was selected 
         //here so I can get the correct ID for that name and set it below. 
         ((EmployeeTime)e.EditingElement.DataContext).ClientContract.SalesActMgrID = 11; 
         doCommit = false; 
        } 
        break; 
      } 
      DataGrid grid = (DataGrid)sender; 
      if (doCommit) 
      { 
       grid.CommitEdit(DataGridEditingUnit.Row, doCommit); 
       EmployeeTime et = e.Row.Item as EmployeeTime; 
       CreateBurdenValue(et); 
      } 
      else 
      { 
       grid.CancelEdit(DataGridEditingUnit.Row); 
      } 
      isManualEditCommit = false; 
     } 
    } 
} 

이 작업을 수행하는 '더 좋은'방법이있을 수 있습니다. 알아두면 좋을 것 같습니다. 적어도 누군가가 커밋 조치가 완료되기 전에 선택된 이름을 얻을 수있는 방향으로 나를 가리킬 수 있다면, 나는 그것을 높이 평가할 것입니다.

정보 만 있으면 셀에서 일반적인 CommitEdit을 수행하면 선택된 이름이 실제로 StaffRole 테이블에서 업데이트되고 있으므로 원래 이름을 표시 한 모든 행에서 변경됩니다. 새로운 선택 이름 (이것은 내가 원하는 것)이 아닙니다.

+0

확실하게, 지난 몇 일간이 답변을 검색했습니다. 내가 이것을 게시 한 다음에 다음 번에 내 검색을 계속할 때 마침내 대답을 찾는다. (e.EditingElement to ComboBox) .SelectionBoxItem – user1741541

답변

0

OP가 댓글에 게시 한 내용을 요약합니다. 편집 핸들러에서 선택한 항목에 액세스하려면 다음을 사용하십시오.

(e.EditingElement as ComboBox).SelectionBoxItem 
관련 문제