在我的 WPF 应用程序中,我生成具有动态列数的数据网格。部分代码:
for (var i = datetime; i < datetime.AddDays(1); i+= TimeSpan.FromHours(1)){
var column = new DataGridTemplateColumn();
column.Header = (i+1).ToString();
column.CellTemplate = (DataTemplate)XamlReader.Load(
new MemoryStream(Encoding.Default.GetBytes(
@"<DataTemplate xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation' xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'><TextBlock Text='{Binding Values[" + i + @"]." + propName + @"}'/></DataTemplate>"
)));
column.CellEditingTemplate = (DataTemplate)XamlReader.Load(
new MemoryStream(Encoding.Default.GetBytes(
@"<DataTemplate xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation' xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'><TextBox Text='{Binding Values[" + i + @"]." + propName + @", Mode=TwoWay}'/></DataTemplate>"
)));
dataGrid1.Columns.Add(column);
}
dataGrid1.ItemsSource = data;
<DataGrid EnableColumnVirtualization="true" EnableRowVirtualization="true" Name="dataGrid1" DockPanel.Dock="Top" AutoGenerateColumns="False"
Height="120" Width="Auto" />
当我的表包含 25 列和 10 行时,它的渲染速度太慢(1 秒)。如何提高数据绑定的性能?