3

DataGridは膨大な量のデータ (平均で最大 4 万行) を処理するため、大量の仮想化が必要です。

場合によっては、特定の列内のセル全体 (すべてではないにしても) を選択して、それらの値をまとめて変更する必要があります。興味のある人のために、列ヘッダー(通常は列をソートします)のクリックを処理し、次のメソッドを呼び出すことでこれを実現します。

private void SelectColumn(object sender, DataGridSortingEventArgs e)
{
    if (MyDataGrid.SelectionUnit != DataGridSelectionUnit.FullRow)
    {
        DataGridColumn column = e.Column;

        if (e.Column != null)
        {
            MyDataGrid.UnselectAllCells();

            for (int i = 0; i < MyDataGrid.Items.Count; i++)
            {
                MyDataGrid.SelectedCells.Add(new DataGridCellInfo(MyDataGrid.Items[i], column));
            }

            // Set the first cell into editing mode
            MyDataGrid.CurrentCell = MyDataGrid.SelectedCells[0];
        }
    }
}

編集:申し訳ありませんが、選択したセルの値を設定するためのコードを追加するのをほとんど忘れていました...:

private void MyDataGrid_CellEditEnding(object sender, DataGridCellEditEndingEventArgs e)
{
    if (MyDataGrid.SelectedCells.Count > 1)
    { // More than 1 cell are selected
        if (e.EditingElement.GetType() == typeof(TextBox))
        { // The cell being edited is of type TextBox
            string value = ((TextBox)e.EditingElement).Text;
            foreach (DataGridCellInfo cellInfo in MyDataGrid.SelectedCells)
            {
                DataGridCell gridCell = TryToFindGridCell(MyDataGrid, cellInfo);
                if (gridCell != null) gridCell.Content = value; // ((TextBox)e.EditingElement).Text returns the Text in the cell sending DataGridCellEditEndingEventArgs e
            }
        }
    }
}

static DataGridCell TryToFindGridCell(DataGrid grid, DataGridCellInfo cellInfo)
{
    DataGridCell result = null;
    DataGridRow row = (DataGridRow)grid.ItemContainerGenerator.ContainerFromItem(cellInfo.Item);
    if (row != null)
    {
        int columnIndex = grid.Columns.IndexOf(cellInfo.Column);
        if (columnIndex > -1)
        {
            DataGridCellsPresenter presenter = GetVisualChild<DataGridCellsPresenter>(row);
            result = presenter.ItemContainerGenerator.ContainerFromIndex(columnIndex) as DataGridCell;
        }
    }
    return result;
}

選択したすべてのセルが GUI の表示領域内にある場合、これは非常にうまく機能します。ただし、外側のすべて (バッファーとしていくつかの行を含む) が仮想化されているため、問題が発生しています。仮想化された行は実際には選択されていません。可視領域外のセルは、可視領域とともに値を変更しません。

誰かがこれに対するより良いアプローチに私を導くことができますか? はい、この量のデータを処理する必要があります。申し訳ありません。;)

4

1 に答える 1