title: WPF Datagrid Performance
body: <p>I am working with the WPF Toolkit data grid and it is scrolling extremely slow at the moment.  The grid has 84 columns and 805 rows. (Including 3 fixed columns and the header is fixed.)  Scrolling both horizontally and vertically is extremely slow.  Virtualization is turned on and I have enabled column virtualization and row virtualization explicitly in the xaml.  Is there anything to watch out for that can really effect performance, such as binding methods, or what xaml is in each celltemplate?</p>
  
  <p>One thing to note is I am dynamically adding the columns on creation of the datagrid.  Could that be effecting anything? (I also dynamically create the celltemplate at the same time so that my bindings are set right.)</p>
  
  <p>Below is the code from the template for most of the cells that get generated.  Basically for the columns I need to dynamically add (which is most of them), I loop through my list and add the columns using the AddColumn method, plus I dynamically build the template so that the binding statements properly index the right item in the collection for that column.  The template isn't too complex, just two TextBlocks, but I do bind four different properties on each.  It seems like I was able to squeeze out a little bit more performance by changes the bindings to OneWay:</p>
  
  <pre><code> private void AddColumn(string s, int index)
      {
          DataGridTemplateColumn column = new DataGridTemplateColumn();
          column.Header = s;
          //Set template for inner cell's two rectangles
          column.CellTemplate = CreateFactViewModelTemplate(index);
          //Set Style for header, ie rotate 90 degrees
          column.HeaderStyle = (Style)dgMatrix.Resources["HeaderRotateStyle"];
          column.Width = DataGridLength.Auto;
          dgMatrix.Columns.Add(column);
      }
  
  
      //this method builds the template for each column in order to properly bind the rectangles to their color
      private static DataTemplate CreateFactViewModelTemplate(int index)
      {
          string xamlTemplateFormat =
              @"&lt;DataTemplate xmlns=""http://schemas.microsoft.com/winfx/2006/xaml/presentation""
              xmlns:x=""http://schemas.microsoft.com/winfx/2006/xaml""&gt;
              &lt;Grid&gt;
              &lt;Grid.ColumnDefinitions&gt;
                  &lt;ColumnDefinition /&gt;
                  &lt;ColumnDefinition /&gt;
              &lt;/Grid.ColumnDefinitions&gt;
              &lt;TextBlock Grid.Column=""0"" MinHeight=""10"" MinWidth=""10"" HorizontalAlignment=""Stretch"" Padding=""3 1 3 1"" TextAlignment=""Center"" Foreground=""{Binding Path=FactViewModels[~Index~].LeftForeColor,Mode=OneWay}"" Background=""{Binding Path=FactViewModels[~Index~].LeftColor,Mode=OneWay}"" Text=""{Binding Path=FactViewModels[~Index~].LeftScore,Mode=OneWay}"" /&gt;
              &lt;TextBlock Grid.Column=""1"" MinHeight=""10"" MinWidth=""10"" HorizontalAlignment=""Stretch"" Padding=""3 1 3 1"" TextAlignment=""Center"" Foreground=""{Binding Path=FactViewModels[~Index~].RightForeColor,Mode=OneWay}"" Background=""{Binding Path=FactViewModels[~Index~].RightColor,Mode=OneWay}"" Text=""{Binding Path=FactViewModels[~Index~].RightScore,Mode=OneWay}"" /&gt;
              &lt;/Grid&gt;
              &lt;/DataTemplate&gt;";
  
  
  
  
          string xamlTemplate = xamlTemplateFormat.Replace("~Index~", index.ToString());
  
          return (DataTemplate)XamlReader.Parse(xamlTemplate);
      }
  </code></pre>