BeginEdit of a specific Cell from code behind

ぐ巨炮叔叔 提交于 2020-01-23 18:56:05

问题


Is possibile begin edit of a specific cell from code behind with DataGrid control (WPF Toolkit)?

I have to enable the celledittemplate of first cell of selected row after a button action...how can I do?


回答1:


pls, try putting the code below in your button's on click event handler:

    DataGridCell cell = GetCell(1, 0);
    if (cell != null)
    {
        cell.Focus();
        yourDataGrid.BeginEdit();
    }

below is implementation for the GetCell method taken from here Grabing controls from a DataGrid

public DataGridCell GetCell(int row, int column)
{
    DataGridRow rowContainer = GetRow(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
            gridPersons.ScrollIntoView(rowContainer, gridPersons.Columns[column]);
            cell = (DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex(column);
        }
        return cell;
    }
    return null;
}

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

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

hope this helps, regards



来源:https://stackoverflow.com/questions/1755799/beginedit-of-a-specific-cell-from-code-behind

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!