2014-01-31 195 views
2

這個問題看起來很簡單,但我無法找到答案。 我有一個DataGrid加載了一些數據。我需要在這個網格的第一行設置焦點。在DataGrid中設置焦點

我將從onClick事件按鈕調用此方法。

myGrid.Focus()不會關注網格中的行。

回答

2

這是在WPF中最難的工作之一,而原因是因爲虛擬化的,事實上,UI渲染線程不同來自運行我們代碼的線程(當UI渲染完成時你無法找到)。 一個完整的參考,你可以使用Dispatcher.Invoke可能作品某些情況下(相信我在WPF Dispatcher.Invoke是你最好的朋友和最大的敵人)

dgGrid.ItemsSource = new List<object>() { new { I = 10, J = 20 }, new { I = 10, J = 20 }, new { I = 10, J = 20 }, new { I = 10, J = 20 }, new { I = 10, J = 20 } }; 
     Dispatcher.Invoke(new Action(delegate() 
     { 
      grd.SelectedIndex = 0; 
      grd.Focus(); 
     } 
    ), System.Windows.Threading.DispatcherPriority.Background); 

已經看這裏 http://social.technet.microsoft.com/wiki/contents/articles/21202.wpf-programmatically-selecting-and-focusing-a-row-or-cell-in-a-datagrid.aspx

或robustest鏈接文章之一

public static DataGridCell GetCell(DataGrid dataGrid, DataGridRow rowContainer, int column) 
{ 
    if (rowContainer != null) 
    { 
     DataGridCellsPresenter presenter = FindVisualChild<DataGridCellsPresenter>(rowContainer); 
     if (presenter == null) 
     { 
      /* if the row has been virtualized away, call its ApplyTemplate() method 
      * to build its visual tree in order for the DataGridCellsPresenter 
      * and the DataGridCells to be created */ 
      rowContainer.ApplyTemplate(); 
      presenter = FindVisualChild<DataGridCellsPresenter>(rowContainer); 
     } 
     if (presenter != null) 
     { 
      DataGridCell cell = presenter.ItemContainerGenerator.ContainerFromIndex(column) as DataGridCell; 
      if (cell == null) 
      { 
       /* bring the column into view 
       * in case it has been virtualized away */ 
       dataGrid.ScrollIntoView(rowContainer, dataGrid.Columns[column]); 
       cell = presenter.ItemContainerGenerator.ContainerFromIndex(column) as DataGridCell; 
      } 
      return cell; 
     } 
    } 
    return null; 
} 

    public static void SelectRowByIndex(DataGrid dataGrid, int rowIndex) 
    { 
     if (!dataGrid.SelectionUnit.Equals(DataGridSelectionUnit.FullRow)) 
      throw new ArgumentException("The SelectionUnit of the DataGrid must be set to FullRow."); 

     if (rowIndex < 0 || rowIndex > (dataGrid.Items.Count - 1)) 
      throw new ArgumentException(string.Format("{0} is an invalid row index.", rowIndex)); 

     dataGrid.SelectedItems.Clear(); 
     /* set the SelectedItem property */ 
     object item = dataGrid.Items[rowIndex]; // = Product X 
     dataGrid.SelectedItem = item; 

     DataGridRow row = dataGrid.ItemContainerGenerator.ContainerFromIndex(rowIndex) as DataGridRow; 
     if (row == null) 
     { 
      /* bring the data item (Product object) into view 
      * in case it has been virtualized away */ 
      dataGrid.ScrollIntoView(item); 
      row = dataGrid.ItemContainerGenerator.ContainerFromIndex(rowIndex) as DataGridRow; 
     } 
     if (row != null) 
    { 
     DataGridCell cell = GetCell(dataGrid, row, 0); 
     if(cell != null) 
      cell.Focus(); 
    } 
} 

您需要添加這些靜態方法調用seco nd一個 它首先嚐試嘗試查找(或繪製行,如果它是繪製,然後設置它的焦點)。

-2
mygrid.SelectedItem = mygrid.Items.Count > 0 ? mygrid.Items[0] : null; 
+0

一句話的答案是*不*在StackOverflow上讚賞。有關詳細信息,請參閱幫助中心的[如何編寫好答案?](http://stackoverflow.com/help/how-to-answer)頁面。 – Sheridan

-2

如果你想集中在電網第一個值嘗試以下

myGrid.SelectedIndex = 0; 
+0

這不關注網格。其實,我們需要關注單元格,就像上面的例子。 – Beetlejuice