Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF DataGrid - How to automatically exit Edit Mode?

I have implemented WPF DataGrid Single-Click Editing from Codeplex. In that solution, the clicked cell is focused and the row is selected to achieve single-click editing of DataGrid. It worked great.

Here's the code:

private void DataGridCell_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
    {
        DataGridCell cell = sender as DataGridCell;
        if (cell != null && !cell.IsEditing && !cell.IsReadOnly)
        {
            if (!cell.IsFocused)
            {
                cell.Focus();
            }
            DataGrid dataGrid = FindVisualParent<DataGrid>(cell);
            if (dataGrid != null)
            {
                if (dataGrid.SelectionUnit != DataGridSelectionUnit.FullRow)
                {
                    if (!cell.IsSelected)
                        cell.IsSelected = true;
                }
                else
                {
                    DataGridRow row = FindVisualParent<DataGridRow>(cell);
                    if (row != null && !row.IsSelected)
                    {
                        row.IsSelected = true;
                    }
                }
            }
        }
    }    

But I also want my DataGrid to automatically exit editing mode (without hitting Enter key) when a cell value is changed. For example, I have a combobox in the cell when in edit mode. When user selects a value in combobox, it will automatically databind the selected value. But then the user still need to click Enter to exit edit mode. How can I exit edit mode automatically?

I've tried listening for property changes and call CommitEdit function of DataGrid to exit edit mode automatically. Works great and here's the code:

 void _gameCompareViewModel_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
    {
        if (e.PropertyName == "End Edit")
        {
            AlignGrid.CommitEdit();
        }

    }

But now the Single-Click editing feature will not work for the current cell. I have to click a different row first in order for it to work. I think what I want is when CommmitEdit is called, it automatically selects a different row. (Like when you hit Enter, it will go to the next row) Any suggestions guys? Please show me codes on how to do this. Im running out of time here for my project.

Thanks for the help.

like image 263
ncfuncion Avatar asked Feb 02 '11 21:02

ncfuncion


People also ask

How to make DataGrid not editable?

You can easily use IsReadOnly property on the DataGrid to make it entirely read only. or use IsReadOnly per Column basis to make them read only. The WPF DataGrid has an IsReadOnly property that you can set to True to ensure that users cannot edit your DataGrid 's cells.

How to make DataGrid editable in WPF?

By default, you can edit items directly in the DataGrid. The user can enter edit mode in a cell by pressing F2 key or double tapping on a cell. Alternatively, you can set the DataGridColumn. IsReadOnly property to true to disable editing in specific columns of the DataGrid.

What is DataGrid WPF?

A DataGrid is a control that displays data in a customizable grid. It provides a flexible way to display a collection of data in rows and columns. The hierarchical inheritance of DataGrid class is as follows −


1 Answers

to flip from cell editing template back to cell template:

dataGrid.CancelEdit();
like image 50
denis morozov Avatar answered Oct 18 '22 19:10

denis morozov