我有一个绑定到名为MyObjects的ObservableCollection的DataGrid.
DataGrid有2列:一列是DataGridTextColumn,另一列是DataGridTemplateColumn.
我想要实现的是在选择单元格时按下键时,模板列的行为类似于文本列.
例如,当您从文本列中选择一个单元格并单击“ A”键时,单元格编辑模板将激活,并且字母“ A”被输入到文本框中.
我想知道的是如何在模板列中实现此行为(即,按键激活其单元格编辑模板,并将字符作为输入传递给模板中的控件).
我的搜索结果只能找到与在单元格之间切换时在编辑模板中获得焦点的控件有关的答案,这与我的问题不同.以下是我的DataGrid的XAML.
<DataGrid ItemsSource="{Binding MyObjects}" AutoGenerateColumns="False">
<DataGrid.Columns>
<DataGridTextColumn Header="Test" Binding="{Binding Test}"/>
<DataGridTemplateColumn Header="Date">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Date}"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
<DataGridTemplateColumn.CellEditingTemplate>
<DataTemplate>
<!--This is the control that I want to focus!-->
<DatePicker SelectedDate="{Binding Date}"/>
</DataTemplate>
</DataGridTemplateColumn.CellEditingTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
编辑:
我编写了一个简单的帮助程序类,该类允许在单元模板加载时聚焦XAML指定的控件……结合Aled的答案,这非常接近我想要的!我只需要弄清楚如何将输入传递给重点控件…
问题在于按键事件是在控件加载事件之前得到处理的,所以我需要弄清楚如何将它们桥接在一起……或完全采用新方法.
public sealed class FrameworkElementFocusHelper
{
private static readonly DependencyProperty FocusOnLoadProperty =
DependencyProperty.RegisterAttached("FocusOnLoad",
typeof(bool),
typeof(FrameworkElementFocusHelper),
new UIPropertyMetadata(FocusOnLoadPropertyChanged));
public static void FocusOnLoadPropertyChanged(DependencyObject source, DependencyPropertyChangedEventArgs e)
{
FrameworkElement element = (FrameworkElement)source;
element.Loaded -= FrameworElementFocusHelperLoadedEvent;
if ((bool)e.NewValue == true)
element.Loaded += FrameworElementFocusHelperLoadedEvent;
}
public static void SetFocusOnLoad(DependencyObject element, bool value)
{
element.SetValue(FocusOnLoadProperty, value);
}
public static bool GetFocusOnLoad(DependencyObject element)
{
return (bool)element.GetValue(FocusOnLoadProperty);
}
public static void FrameworElementFocusHelperLoadedEvent(object sender, RoutedEventArgs e)
{
((FrameworkElement)sender).Focus();
}
}
用法:
<DataGridTemplateColumn.CellEditingTemplate>
<DataTemplate>
<DatePicker SelectedDate="{Binding Date}" rt:FrameworkElementFocusHelper.FocusOnLoad="true"/>
</DataTemplate>
</DataGridTemplateColumn.CellEditingTemplate>
解决方法:
我有一种方法,至少可以让您在按键时进入编辑模式.
首先,这里是我拥有的扩展类,它提供了一些方法以编程方式获取行/列(在这种情况下可能不需要所有方法):
namespace MyApp.Extensions
{
/// <summary>
/// Helper methods for the WPF DataGrid.
/// </summary>
public static class DataGridExtensions
{
/// <summary>
/// Gets a specific row from the data grid. If the DataGrid is virtualised the row will be scrolled into view.
/// </summary>
/// <param name="grid">The DataGrid.</param>
/// <param name="rowIndex">Row number to get.</param>
/// <returns></returns>
public static DataGridRow GetRow(this DataGrid grid, int rowIndex)
{
var row = (DataGridRow)grid.ItemContainerGenerator.ContainerFromIndex(rowIndex);
if (row == null)
{
grid.UpdateLayout();
grid.ScrollIntoView(grid.Items[rowIndex]);
row = (DataGridRow)grid.ItemContainerGenerator.ContainerFromIndex(rowIndex);
}
return row;
}
/// <summary>
/// Get the selected row.
/// </summary>
/// <param name="grid">DataGridRow.</param>
/// <returns>DataGridRow or null if no row selected.</returns>
public static DataGridRow GetSelectedRow(this DataGrid grid)
{
return (grid.SelectedIndex) < 0 ? null : (DataGridRow)grid.ItemContainerGenerator.ContainerFromIndex(grid.SelectedIndex);
}
/// <summary>
/// Gets a specific cell from the DataGrid.
/// </summary>
/// <param name="grid">The DataGrid.</param>
/// <param name="row">The row from which to get a cell from.</param>
/// <param name="column">The cell index.</param>
/// <returns>A DataGridCell.</returns>
public static DataGridCell GetCell(this DataGrid grid, DataGridRow row, int column)
{
if (row == null) return null;
var presenter = GetVisualChild<DataGridCellsPresenter>(row);
if (presenter == null)
{
// Virtualised - scroll into view.
grid.ScrollIntoView(row, grid.Columns[column]);
presenter = GetVisualChild<DataGridCellsPresenter>(row);
}
return (DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex(column);
}
/// <summary>
/// Gets a specific cell from the DataGrid.
/// </summary>
/// <param name="grid">The DataGrid.</param>
/// <param name="row">The row index.</param>
/// <param name="column">The cell index.</param>
/// <returns>A DataGridCell.</returns>
public static DataGridCell GetCell(this DataGrid grid, int row, int column)
{
var rowContainer = grid.GetRow(row);
return grid.GetCell(rowContainer, column);
}
/// <summary>
/// Gets the currently selected (focused) cell.
/// </summary>
/// <param name="grid">The DataGrid.</param>
/// <returns>DataGridCell or null if no cell is currently selected.</returns>
public static DataGridCell GetSelectedCell(this DataGrid grid)
{
var row = grid.GetSelectedRow();
if (row != null)
{
for (int i = 0; i < grid.Columns.Count; i++)
{
var cell = grid.GetCell(row, i);
if (cell.IsFocused)
return cell;
}
}
return null;
}
/// <summary>
/// Helper method to get a particular visual child.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="parent"></param>
/// <returns></returns>
private 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++)
{
var v = (Visual)VisualTreeHelper.GetChild(parent, i);
child = v as T ?? GetVisualChild<T>(v);
if (child != null)
{
break;
}
}
return child;
}
}
}
现在,将处理程序添加到Datagrid上的PreviewKeyDown事件中.
<DataGrid ItemsSource="{Binding MyData}" PreviewKeyDown="MyDataGrid_OnPreviewKeyDown">
这是处理程序:
private void MyDataGrid_OnPreviewKeyDown(object sender, KeyEventArgs e)
{
var dg = sender as DataGrid;
// alter this condition for whatever valid keys you want - avoid arrows/tab, etc.
if (dg != null && !dg.IsReadOnly && e.Key == Key.Enter)
{
var cell = dg.GetSelectedCell();
if (cell != null && cell.Column is DataGridTemplateColumn)
{
cell.Focus();
dg.BeginEdit();
e.Handled = true;
}
}
}
有点麻烦,但似乎起作用.将按键传递到编辑控件上可能不太难.
我确实通过创建自己的DataGridXYZColumn类来尝试另一种方式,但是有一个主要的陷阱,那就是用于处理键盘输入的方法被标记为内部方法,并且不可替代,因此我只剩下这种方法!