DataGrid Cell.Style binding

前端 未结 2 1566
生来不讨喜
生来不讨喜 2021-01-24 15:37

I have a performance issue with the WPF DataGrid (.net 4.0)

first, some details:

  • I have a datagrid with an Observable collection as ItemsSource.
  • t
2条回答
  •  萌比男神i
    2021-01-24 16:19

    If ColumnVirtualization make so problems, why do you need it? You can do a several improvements, but they can't solve the problem completely.

    1. Change TextBoxes for light-weight objects:

      public class TextItem
      {
          public string Text { get; set; }
          public Brush Background { get; set; }
          public Brush Foreground { get; set; }
      }
      
      
      public class Row : ObservableCollection
      {
      }
      
    2. Enable VirtualizingStackPanel: dg.SetValue(VirtualizingStackPanel.IsVirtualizingProperty, true);

    3. Replace styles with templates:

          for (int i = 0; i < 100; i++)
          {
              DataGridTemplateColumn column = new DataGridTemplateColumn();
              column.CellTemplate = (DataTemplate)XamlReader.Parse(
                  "" +
                      "" +
                  "");
              column.Header = "Column " + i;
              dg.Columns.Add(column);
          }
      

提交回复
热议问题