WPF DataGrid:关于在可见区域之外虚拟化选定单元的问题

本文关键字:虚拟化 单元 问题 区域 DataGrid WPF | 更新日期: 2023-09-27 18:04:18

我的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的可见区域内,这将非常有效。但是,由于外部的所有内容(以几行作为缓冲区)都被虚拟化了,因此我遇到了问题。虚拟行并没有真正被选中,可见区域之外的任何单元格都不会随着可见区域的值而改变它们的值。

谁能给我一个更好的方法?是的,我需要处理这些数据量,对不起。div;)

我最近在使用WPF的数据网格时遇到了同样的问题。它太慢了。我认为这是因为datagridrow对象对于这个数据量来说太臃肿了。

在我的例子中,我必须处理的结果数量非常相似。我通过完全取消数据网格解决了这个问题。我希望您的列的#是恒定的,因为这将是一个痛苦(阅读:我不确定如何:))实现这种方式,如果不是这样的话…无论如何…

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

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

等等……

无论你从哪里加载数据,遍历datatable/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绑定到我们刚刚创建的视图模型中的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>

通过这种方式,可以关闭虚拟化,因为它不像数据网格的对象那么胖,所以性能要好得多。现在,无需虚拟化,您所选择的内容确实被选中了,并且您可以理智地遍历它。

WPF DataGrid:关于在可见区域之外虚拟化选定单元的问题