编辑模式SilverLight DataGrid中的选项卡导航

时间:2014-03-31 13:56:13

标签: c# wpf silverlight xaml datagrid

我正在尝试为SilverLight DataGrid创建标签导航(sdk:DataGrid)。如您所知,DataGrid没有这样的导航,但是如果您按F2,则编辑一个单元格,然后按Tab,然后在编辑模式中选择下一个单元格:

Image of the DataGrid

图像描述:我选择了第一行的第二列(名称),我编辑了这个单元格(按F2),之后我按了Tab,你可以看到下一个单元格在编辑模式中选择。

但实际上我的B列包含ButtonImage(不幸的是,现在我不使用我的计算机,我无法使用Button演示我的DataGrid),如果我按F2获取Date单元格,然后按Tab,然后下一个单元格的Button消失,我无法使用箭头或Tab继续导航(如虽然DataGrid挂起)。

如果我覆盖DatGrid.KeyUp:

void MyGrid_KeyUp(object sender, KeyEventArgs e)
{
  if (e.Key == Key.Tab)
  {
    //do nothing              
  }
}

如果我使用F2Tab,那么DataGrid具有与之前相同的行为。

我如何做到以下几点:

1)按F2; 2)如果在F2之后按Tab,则下一个单元格不会像这种情况一样进入编辑模式。我希望在Tab之后,下一个单元格将在没有编辑的情况下聚焦,就像我使用鼠标单击单元格一样。

谢谢!

P.S。如果我覆盖LostFocus

void MyGrid_LostFocus(object sender, RoutedEventArgs e)
{
  MyGrid.CommitEdit();
}

它不能解决包含Button的列的问题。

1 个答案:

答案 0 :(得分:0)

我已使用CurrentCellChanged解决了该问题,请参阅以下示例代码:

/// <summary>
/// Checks if current column contains graphic elements, focuses next cell of next column.
/// </summary>
/// <param name="dg">Source DataGrid</param>
private static void CurrentCellChangedHandler(DataGrid dg)
{
  if (isCurrentCellContainsGraphicElement(dg))
  {
    FocusNextCell(dg);
  }
}

/// <summary>
/// Checks if current cell contains a graphic element.
/// </summary>
/// <param name="dg">Source DataGrid</param>
/// <returns>true if current column contains graphic elements, else - false</returns>
private static bool isCurrentCellContainsGraphicElement(DataGrid dg) 
{
  var box = DataGridExcelNavigation.GetCellItem<TextBox>(dg.SelectedItem, dg.CurrentColumn);

  if (box != null) return false;

  return true;
}

private static T GetCellItem<T>(object item, DataGridColumn column) where T : class
{
  if (item == null) return null;

  var cellData = (column.GetCellContent(item) as T);
  if (cellData == null)
  {
    var gridData = (column.GetCellContent(item) as Panel);
    if (gridData != null)
    {
      cellData = (gridData.Children.Where(x => x.GetType() == typeof(T)).FirstOrDefault() as T);
    }
  }
  return cellData;
}