3

DataGrid处理大量数据(平均多达 40k 行),因此我需要进行大量虚拟化。

有时我需要选择某一列中的一大堆(如果不是全部)单元格,以共同更改它们的值。对于任何感兴趣的人,我通过单击列标题(通常对列进行排序)并调用以下方法来实现这一点:

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

1

我最近对 ​​WPF 的数据网格有同样的问题。它太慢了。我认为这是因为 datagridrow 对象对于这么多的数据来说太臃肿了。

就我而言,我必须处理的结果数量非常相似。我通过完全取消数据网格来解决它。我希望你的列数是不变的,因为如果不是这样的话,以这种方式实现会很痛苦(阅读:我不确定如何:))......无论如何......

创建一个类并为数据表中的每一列创建一个属性

class TableItem
{
    public string Column1 { get; set: }
    public string Column2 { get; set; }
}

ETC....

无论您从哪里加载数据,遍历数据表/等并将列添加到视图模型中的 ObservableCollection 对象。

for (int = 0; i < yourDataTable.Rows.Count; i++)
{
    DataRow row = yourDataTable.Rows[i];
    TableItem ti = new TableItem 
    { 
        Column1 = row["Column1Name"].ToString(), 
        Column2 = row["Column2Name"].ToString()
    }
    yourObservableCollection.Add(ti);
}

ObservableCollectionInViewModel = yourObservableCollection;

然后创建一个 ListView,并给它你需要的任意数量的列。将 ListView 绑定到我们刚刚在上面创建的 viewmodel 中的 ObservableCollection,然后使用 DisplayMemberBinding 将每一列绑定到 TableItem 对象的相应属性:

<ListView x:Name="lstGridWOResourceHogging" SelectionMode="Extended" ItemsSource="{Binding ObservableCollecitonInViewModel, IsAsync="True"
              VirtualizingPanel.IsVirtualizing="False">
        <ListView.View>
            <GridView>
                <GridViewColumn Header="Column1Name" DisplayMemberBinding="{Binding Column1}"/>
                <GridViewColumn Header="Column2Name" DisplayMemberBinding="{Binding Column2}"/>
            </GridView>
        </ListView.View>
    </ListView>

这样,您可以关闭虚拟化,因为它不像数据网格的对象那么胖,所以性能要好得多。现在,在没有虚拟化的情况下,您真正​​选择的内容已被选中,您可以理智地迭代它。

于 2017-10-12T22:08:58.353 回答