简体   繁体   中英

Can you retrieve the datagrid object with just a DataGridRow? WPF

I'm trying to retrieve the datagrid because I want to focus on a specific cell in a row. I have a DataGridRow based on the LoadingRow event that I use by doing this:

<i:EventTrigger EventName="LoadingRow">
   <utils:InteractiveCommand Command="{Binding RelativeSource = {RelativeSource FindAncestor, AncestorType={x:Type Window}}, Path=DataContext.MainWindowViewModel.SDataGrid_LoadingRow}"/>
</i:EventTrigger>

But in the function receiving this, I only am able to get the DataGridRow.

    public void SDataGridLoadingRow(object param)
    {
        DataGridRowEventArgs e = param as DataGridRowEventArgs;
        e.Row.Tag = e.Row.GetIndex().ToString();
    }

I want to get a specific cell from the row and focus on it so the user can type. Is this possible?

I'm using MVVM

Also have this now

    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);
    }

And the previous row is still in edit mode... can't quite figure out how to get it out of edit mode...

I am not sure why your LoadingRow event handler is in your ViewModel. If you are using MVVM your viewModels shouldn't be manipulating visual element such as DataGrid and DataGridCell but only the underlying business data.

In your case you could subscribe to the LoadingRow event like this:

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

and then in your code behind (xaml.cs file):

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.
        }
    }

What you can do is subscribe to the loaded event of your grid and set the selectedCell there:

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;
    }

You could also execute the same logic with a button.

Xaml:

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

And if some of the treatment is related to the business and needs to be in your viewModel. You can then invoke a command or run public methods from DataGrid_LoadingRow in your code behind.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM