【问题标题】:Can you retrieve the datagrid object with just a DataGridRow? WPF您可以仅使用 DataGridRow 检索数据网格对象吗? WPF
【发布时间】: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);
    }

而上一行仍处于编辑模式...无法完全弄清楚如何使其退出编辑模式...

【问题讨论】:

标签: c# wpf


【解决方案1】:

我不确定为什么您的 LoadingRow 事件处理程序在您的 ViewModel 中。 如果您使用 MVVM,则您的 viewModel 不应操作可视元素,例如 DataGrid 和 DataGridCell,而应仅操作底层业务数据。

在您的情况下,您可以像这样订阅 LoadingRow 事件:

<DataGrid ItemsSource="{Binding BusinessObjectExemples}" LoadingRow="DataGrid_LoadingRow" />

然后在你的代码后面(xaml.cs 文件):

private void DataGrid_LoadingRow(object sender, DataGridRowEventArgs e)
    {
        if (sender is DataGrid dataGrid && e.Row is DataGridRow row)
        {
            //You can now access your dataGrid and the row
            row.Tag = row.GetIndex().ToString();
            //The grid is still loading row so it is too early to set the current cell.
        }
    }

您可以做的是订阅网格的加载事件并在那里设置 selectedCell:

private void Grid_Loaded(object sender, RoutedEventArgs e)
    {
        //Adapt the logic for the cell you want to select
        var dataGridCellInfo = new DataGridCellInfo(this.Grid.Items[11], this.Grid.Columns[1]);
        //The grid must be focused in order to be directly editable once a cell is selected
        this.Grid.Focus();
        //Setting the SelectedCell might be neccessary to show the "Selected" visual
        this.Grid.SelectedCells.Clear();
        this.Grid.SelectedCells.Add(dataGridCellInfo);

        this.Grid.CurrentCell = dataGridCellInfo;
    }

您也可以使用按钮执行相同的逻辑。

Xaml:

<DataGrid x:Name="Grid" ItemsSource="{Binding BusinessObjectExemples}" 
                  Loaded="Grid_Loaded" SelectionUnit="Cell" AutoGenerateColumns="False" 
                  LoadingRow="DataGrid_LoadingRow">

如果某些处理与业务相关并且需要在您的 viewModel 中。然后,您可以在后面的代码中从 DataGrid_LoadingRow 调用命令或运行公共方法。

【讨论】:

  • 嗨 Ostas,感谢您的回答。我正在使用 Caliburn Micro。如果我将它放在代码隐藏中,它将进入 ShellView.xaml.cs 而不是 MainWindowViewModel.cs 类。这样可以吗?
  • Ostas,我注意到,焦点确实发生了变化,但我必须按键盘上的“enter”才能开始打字。你知道为什么吗?
  • 我对 Caliburn micro 不熟悉,但caliburnmicro.com 提到了 ViewModel 的去耦,所以我认为它变化不大。我想你的视图被称为 ShellView.xaml,所以这将是这种操作的正确位置。如果您不确定什么属于 viewModel 和什么属于 view,请检查 en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93viewmodel。关于你必须按回车的事实,我想你的单元格中有一个 TextBox,你应该把焦点放在 TextBox 而不是单元格上。
  • 嗨,奥斯拉斯,谢谢。我在单元格内没有文本框...我执行以下操作:
  • 我注意到当为下一行触发加载行函数时,前一行的文本仍然是焦点(我通过执行 BeginEdit 并使用调度程序调用它解决了焦点问题),但是我没有'不明白为什么上一行仍处于编辑模式。我什至尝试在将下一行设置为 BeginEdit 之前执行 currentcell cancelEdit 但上一行仍在编辑:\
猜你喜欢
  • 2013-08-18
  • 1970-01-01
  • 1970-01-01
  • 2013-11-02
  • 1970-01-01
  • 2011-03-16
  • 1970-01-01
  • 1970-01-01
  • 2013-03-03
相关资源
最近更新 更多