1

DataGridコントロール(WPFツールキット)を使用して、コードビハインドから特定のセルの編集を開始することは可能ですか?

ボタンアクションの後に、選択した行の最初のセルのcelledittemplateを有効にする必要があります...どうすればよいですか?

4

1 に答える 1

12

pls は、ボタンの on click イベント ハンドラーに以下のコードを入れてみてください。

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

以下は、ここから取得した GetCell メソッドの実装です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;
} 

これが役に立てば幸いです、よろしく

于 2009-11-19T04:37:12.107 に答える