我目前正在使用 DataGrid。大约 24 列是在 C# 中动态创建的。
我的 DataGrid 中总是有大约 300 个条目(由于一个条目代表一个“标题”,我无法创建分页系统,因为我必须在同一页面中获取所有数据)。
它工作得很好,但是如果我使用DataGridTemplateColumn
s(因为我需要一个样式化的列标题,它有一个分隔符和 2 个标题,因为我需要每列上有 2 个子列)和单元格模板(仍然因为我需要这 2 个子列),有一个双重绑定(每个子列一个绑定),当我加载网格时,它只是无法使用......
我尝试了所有类型的虚拟化(StackPanel、RowVirtualization、ColumnVirtualization 以及所有不同类型的值组合)。我可以获得的“最佳”性能是将 RowVirtualization 和 ColumnVirtualization 设置为 True。
它现在“可用”,但是当我进行水平滚动时仍然很慢(即使有一点图形错误,因为我使用了 FrozenColumn ......)
我什至尝试使用我自己的 ListView / GridView,并且在工作了几个小时之后(为了重现冻结的列等......)仍然存在相同的“问题”。
无法使用数据虚拟化(因为“只有”24 列和 285 行,所以对用户来说根本不友好)。
谢谢 !
编辑 1:这是生成列的代码
ColumnCollection = new ObservableCollection<DataGridColumn>();
DataGridTemplateColumn firstDtc_l = new DataGridTemplateColumn();
firstDtc_l.Header = "Titles";
FrameworkElementFactory spFactory_l = new FrameworkElementFactory(typeof(Grid));
ColumnCollection.Add(firstDtc_l);
int i = 0;
foreach (string s in DynamicColumns)
{
DataGridTemplateColumn dtc_l = new DataGridTemplateColumn();
Binding bindColor = new Binding();
bindColor.Converter = new ChangedColorConverter();
bindColor.ConverterParameter = "Column" + i;
//DataTemplate
DataTemplate dt_l = new DataTemplate("MyObject");
spFactory_l = new FrameworkElementFactory(typeof(Grid));
spFactory_l.Name = "CellTemplate";
FrameworkElementFactory columnDefinition1 = new FrameworkElementFactory(typeof(ColumnDefinition));
FrameworkElementFactory columnDefinition2 = new FrameworkElementFactory(typeof(ColumnDefinition));
FrameworkElementFactory border1 = new FrameworkElementFactory(typeof(Border));
border1.SetValue(Grid.ColumnProperty, 0);
border1.SetValue(Border.BorderBrushProperty, Brushes.Gray);
border1.SetValue(Border.BorderThicknessProperty, new Thickness(0,0,0,0));
FrameworkElementFactory border2 = new FrameworkElementFactory(typeof(Border));
border2.SetValue(Grid.ColumnProperty, 1);
border2.SetValue(Border.BorderBrushProperty, Brushes.Gray);
border2.SetValue(Border.BorderThicknessProperty, new Thickness(1, 0, 0, 0));
FrameworkElementFactory textBlock1 = new FrameworkElementFactory(typeof(TextBlock));
textBlock1.SetValue(Grid.ColumnProperty, 0);
textBlock1.SetValue(TextBlock.ForegroundProperty, bindColor);
Binding firstBind = new Binding("MyObject[Column"+i+"].FirstBinding");
textBlock1.SetValue(TextBlock.TextProperty, localBind);
FrameworkElementFactory textBlock2 = new FrameworkElementFactory(typeof(TextBlock));
Binding secongBind = new Binding("MyObject[Column" + i + "].SecondBinding");
textBlock2.SetValue(Grid.ColumnProperty, 0);
textBlock2.SetValue(TextBlock.TextProperty, firstBind)
textBlock2.SetValue(TextBlock.ForegroundProperty, secongBind);
border1.AppendChild(textBlock1);
border2.AppendChild(textBlock2);
spFactory_l.AppendChild(columnDefinition1);
spFactory_l.AppendChild(columnDefinition2);
spFactory_l.AppendChild(border1);
spFactory_l.AppendChild(border2);
dt_l.VisualTree = spFactory_l;
dtc_l.Width = DataGridLength.Auto;
dtc_l.CellTemplate = dt_l;
dtc_l.Header = s;
ColumnCollection.Add(dtc_l);
i++;
}
DataGrid 绑定到“TheObject”的集合。TheObject 类有一个public Dictionary<string, MyCell> MyObject { get; set; }
MyCell 类,它具有 FirstBinding 和 SecondBinding 属性(字符串)。