【发布时间】:2018-07-24 13:39:53
【问题描述】:
我有一个简单的 WPF 应用程序,它在数据网格中显示大约 30-35,000 行数据,如果用户更改任何单元格值,则应通过更改其背景颜色突出显示该单元格。列在运行时根据数据生成。鉴于它拥有的数据量,虚拟化数据网格是必须的。这是我的数据网格:
<DataGrid x:Name="dgPropertyData" HeadersVisibility="Column" ColumnHeaderHeight="25"
BorderThickness="0" AutoGenerateColumns="false"
HorizontalGridLinesBrush="#FFC7C5C5"
VerticalGridLinesBrush="#FFC7C5C5"
MinRowHeight="20" CanUserResizeRows="False"
CanUserDeleteRows="False" CanUserReorderColumns="False"
EnableRowVirtualization="true"
AutoGeneratedColumns="dgPropertyData_AutoGeneratedColumns"
CellEditEnding="dgPropertyData_CellEditEnding">
<DataGrid.ColumnHeaderStyle>
<Style TargetType="{x:Type DataGridColumnHeader}">
<Setter Property="FontWeight" Value="Bold"/>
</Style>
</DataGrid.ColumnHeaderStyle>
</DataGrid>
我正在代码隐藏方法中更改单元格背景颜色:
private void dgPropertyData_CellEditEnding(object sender, DataGridCellEditEndingEventArgs e)
{
string oldValue = (e.Row.Item as BindableDynamicDictionary)[e.Column.Header as string] as string;
string newValue = (e.EditingElement as System.Windows.Controls.TextBox).Text;
if ((oldValue != null && oldValue.CompareTo(newValue) != 0) || (oldValue == null && string.IsNullOrEmpty(newValue) == false))
{
(e.Column.GetCellContent(e.Row).Parent as DataGridCell).Background = Brushes.Orange;
IsCellEdited = true;
}
}
我的问题:当我编辑一个单元格时,背景会很好地变化以通知值已更改。直到......我滚动网格视图。滚动时, 已编辑的单元格被擦除,错误的单元格被绘制(如果有的话)。
我通过以下问题得到了一些想法:
- Changing color of a single cell in WPF Datagrid
- Wpf Datagrid Virtualization Issue when setting cell colors
- RowVirtualization cause incorrect background color for rows
但是还没有找到适合我的目的的解决方案。作为 WPF 世界的新手,我经常会思考如何解决这个问题。我知道丢失单元格背景颜色的问题是由于行的回收,如果我禁用虚拟化,性能损失对于我拥有的数据量是无法承受的。
【问题讨论】:
标签: wpf datagrid wpfdatagrid