【发布时间】:2020-05-01 19:36:31
【问题描述】:
我正在尝试检索数据网格,因为我想关注一行中的特定单元格。我有一个基于我通过这样做使用的 LoadingRow 事件的 DataGridRow:
<i:EventTrigger EventName="LoadingRow">
<utils:InteractiveCommand Command="{Binding RelativeSource = {RelativeSource FindAncestor, AncestorType={x:Type Window}}, Path=DataContext.MainWindowViewModel.SDataGrid_LoadingRow}"/>
</i:EventTrigger>
但是在接收这个的函数中,我只能得到 DataGridRow。
public void SDataGridLoadingRow(object param)
{
DataGridRowEventArgs e = param as DataGridRowEventArgs;
e.Row.Tag = e.Row.GetIndex().ToString();
}
我想从行中获取特定的单元格并专注于它,以便用户可以键入。这可能吗?
我正在使用 MVVM
现在也有这个
public void SDataGridLoadingRow(object sender, DataGridRowEventArgs e)
{
e.Row.Tag = e.Row.GetIndex().ToString();
DataGrid dataGrid = sender as DataGrid;
dataGrid.Focus();
// Cancel our focus from the current cell of the datagrid
// if there is a current cell
if (dataGrid.CurrentCell != null)
{
var cancelEdit = new System.Action(() =>
{
dataGrid.CancelEdit();
});
Application.Current.Dispatcher.BeginInvoke(cancelEdit,
System.Windows.Threading.DispatcherPriority.ApplicationIdle, null);
}
dataGrid.CurrentCell = new DataGridCellInfo(
dataGrid.Items[e.Row.GetIndex()], dataGrid.Columns[1]);
var startEdit = new System.Action(() =>
{
dataGrid.BeginEdit();
});
Application.Current.Dispatcher.BeginInvoke(startEdit,
System.Windows.Threading.DispatcherPriority.ApplicationIdle, null);
}
而上一行仍处于编辑模式...无法完全弄清楚如何使其退出编辑模式...
【问题讨论】:
-
这能回答你的问题吗? WPF - How to get a cell from a DataGridRow?