2014-03-18 2 views
0

DataGrid에 대한 간단한 데이터 바인딩을 수행했습니다. 이제 행을 클릭하면 DataGrid에서 관련 rowdata (전체 행 데이터)를 얻고 싶습니다. 행 선택 이벤트가 없으므로 마우스 키보드를 사용해야합니까?.Datagrid 행 선택 이벤트, WPF

답변

0
private void dataGrid_SelectionChanged(object sender, SelectionChangedEventArgs e) 
{ 
    ProductItem productItem = (ProductItem)dataGrid.SelectedItem; //Datagrid bound with ProductItem 
} 
1

나는 이렇게했다. 다른 것들은 더 간단한 방법 일 수 있습니다! Mine은 Mediaplayer에 대한 PlayListEntries 클래스의 Observable Collection을 처리합니다. 도움이 되었으면 좋겠다.

private void PlayList_OnMouseLeftButtonUp(object sender, MouseButtonEventArgs e) 
    { 
     DependencyObject dep = (DependencyObject)e.OriginalSource; 

     // iteratively traverse the visual tree 
     while ((dep != null) && !(dep is DataGridCell) && !(dep is DataGridColumnHeader)) 
     { 
      dep = VisualTreeHelper.GetParent(dep); 
     } 

     if (dep == null) 
      return; 

     if (dep is DataGridColumnHeader) 
     { 
      DataGridColumnHeader columnHeader = dep as DataGridColumnHeader; 
      // do something 
     } 

     if (dep is DataGridCell) 
     { 
      DataGridCell cell = dep as DataGridCell; 

      // navigate further up the tree 
      while ((dep != null) && !(dep is DataGridRow)) 
      { 
       dep = VisualTreeHelper.GetParent(dep); 
      } 

      DataGridRow row = dep as DataGridRow; 

      var ple = (PlayListEntry)row.Item; 

// From here you have access to all of the row. 
// Each column is suitable bound. 
// I can post the xaml if you are not sure. 

      //object value = ExtractBoundValue(row, cell); //4 

      //int columnIndex = cell.Column.DisplayIndex; 
      //int rowIndex = FindRowIndex(row); 

      //var s = string.Format("Cell clicked [{0}, {1}] = {2}",rowIndex, columnIndex, value.ToString()); 
     } 
    }