2012-11-02 177 views
0

我的窗口中有一些按鈕和一個datagrid。WPF DataGrid在關閉最後一個單元格時丟失焦點

<Button>Save</Button> 
<Button>Back</Button> 
<DataGrid x:Name="data" ItemsSource="{Binding Scores}" /> 

當我編輯NewItemPlaceholder行,改變了第一個值(但沒有新的NewItemPlaceholder行)時,創建新項目時。當我編輯上一個值並用Tab鍵移動時,會生成一個新行。 但是光標移動到保存按鈕而不是新行中的第一個單元格。

我該如何關注網格?

爲了完整:我使用ObservableCollection作爲ItemsSource。

回答

1

我在這裏找到了解決方案:Customize focus behavior after a row commit through the DataGrid.RowEditEnding event

private void data_RowEditEnding_1(object sender, DataGridRowEditEndingEventArgs e) { 
    if (e.EditAction == DataGridEditAction.Commit) { 
     if (e.Row.Item == data.Items[data.Items.Count - 2]) { 
      var rowToSelect = data.Items[data.Items.Count - 1]; 
      int rowIndex = data.Items.IndexOf(rowToSelect); 
      this.Dispatcher.BeginInvoke(new DispatcherOperationCallback((param) => { 
       var cell = DataGridHelper.GetCell(data, rowIndex, 0); 
       cell.Focus(); 
       data.BeginEdit(); 
       return null; 
      }), DispatcherPriority.Background, new object[] { null }); 
     } 
    } 
} 

使用GetCell和GETROW方法從這裏:datagrid get cell index

static class DataGridHelper { 
    static public DataGridCell GetCell(DataGrid dg, int row, int column) { 
     DataGridRow rowContainer = GetRow(dg, row); 

     if (rowContainer != null) { 
      DataGridCellsPresenter presenter = GetVisualChild<DataGridCellsPresenter>(rowContainer); 

      // try to get the cell but it may possibly be virtualized 
      DataGridCell cell = (DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex(column); 
      if (cell == null) { 
       // now try to bring into view and retreive the cell 
       dg.ScrollIntoView(rowContainer, dg.Columns[column]); 
       cell = (DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex(column); 
      } 
      return cell; 
     } 
     return null; 
    } 

    static public DataGridRow GetRow(DataGrid dg, int index) { 
     DataGridRow row = (DataGridRow)dg.ItemContainerGenerator.ContainerFromIndex(index); 
     if (row == null) { 
      // may be virtualized, bring into view and try again 
      dg.ScrollIntoView(dg.Items[index]); 
      row = (DataGridRow)dg.ItemContainerGenerator.ContainerFromIndex(index); 
     } 
     return row; 
    } 

    static T GetVisualChild<T>(Visual parent) where T : Visual { 
     T child = default(T); 
     int numVisuals = VisualTreeHelper.GetChildrenCount(parent); 
     for (int i = 0; i < numVisuals; i++) { 
      Visual v = (Visual)VisualTreeHelper.GetChild(parent, i); 
      child = v as T; 
      if (child == null) { 
       child = GetVisualChild<T>(v); 
      } 
      if (child != null) { 
       break; 
      } 
     } 
     return child; 
    } 
} 
3

使用KeyboardNavigation.TabNavigation附加屬性:

<DataGrid x:Name="data" 
      ItemsSource="{Binding Scores}" 
      KeyboardNavigation.TabNavigation="Cycle" /> 
+0

謝謝,解決了失去焦點的部分。但現在第一行被選中而不是最後一行。 – dwonisch

0

Here is an alternative包裝在這樣的aw您可以在DataGrid的默認樣式中啓用它並將其應用於整個應用程序。

相關問題