Quantcast
Channel: Windows Presentation Foundation (WPF) forum
Viewing all articles
Browse latest Browse all 18858

Programmatically beginning edit in DataGrid cell

$
0
0
Hi group, I'm trying to customize the behaviour of a data-bound WPF Toolkit DataGrid so that editing is automatically begun whenever the user changes selection. Each row contains just 3 columns and only the first is editable. I tried to handle the datagrid selectionchanged event, so that in code behind I can get the current row and then the 1st cell in it, focus it and begin edit programmatically. Anyway This works only partially as editing is begun but if I type something nothing happens as the focus isn't really there and the caret is missing from the textbox. If I manually click in the textbox I can effectively type, but the point was to avoid clicking and just let editing happen. Could anyone give a hint? here's the relevant code for the handler and its helper methods:

DataGridCell cell = DataGridHelper.GetCell(mygrid, mygrid.SelectedIndex, 0);
cell.Focus();            
mygrid.BeginEdit();

The DataGridHelper is implemented as follows:

static class DataGridHelper
{
    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++)
        {
            Visual v = (Visual)VisualTreeHelper.GetChild(parent, i);
            child = v as T;
            if (child == null)
            {
                child = GetVisualChild<T>(v);
            }
            if (child != null)
            {
                break;
            }
        }
        return child;
    } 

    static public DataGridCell GetCell(DataGrid dg, int row, int column)
    {
        DataGridRow rowContainer = GetRow(dg, row);

        if (rowContainer != null)
        {
            DataGridCellsPresenter presenter = GetVisualChild<DataGridCellsPresenter>(rowContainer);

            // try to get the cell but it may possibly be virtualized
            DataGridCell cell = (DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex(column);
            if (cell == null)
            {
                // now try to bring into view and retreive the cell
                dg.ScrollIntoView(rowContainer, dg.Columns[column]);
                cell = (DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex(column);
            }
            return cell;
        }
        return null;
    }


    static public DataGridRow GetRow(DataGrid dg, int index)
    {
        DataGridRow row = (DataGridRow)dg.ItemContainerGenerator.ContainerFromIndex(index);
        if (row == null)
        {
            // may be virtualized, bring into view and try again
            dg.ScrollIntoView(dg.Items[index]);
            row = (DataGridRow)dg.ItemContainerGenerator.ContainerFromIndex(index);
        }
        return row;
    }
}

Viewing all articles
Browse latest Browse all 18858

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>