{"id":2971,"date":"2015-03-22T16:28:39","date_gmt":"2015-03-22T16:28:39","guid":{"rendered":"http:\/\/putridparrot.com\/blog\/?p=2971"},"modified":"2015-03-22T16:28:39","modified_gmt":"2015-03-22T16:28:39","slug":"the-column-background-colour-in-my-xamdatagrid-keeps-changing","status":"publish","type":"post","link":"https:\/\/putridparrot.com\/blog\/the-column-background-colour-in-my-xamdatagrid-keeps-changing\/","title":{"rendered":"The column background colour in my XamDataGrid keeps changing"},"content":{"rendered":"<p>The column background colour in my XamDataGrid keeps changing or to put it another way, beware the CellContainerGenerationMode when overriding the CellValuePresenterStyle.<\/p>\n<p><strong>What&#8217;s the problem ?<\/strong><\/p>\n<p><em>First off, please note I&#8217;m using v12.2 of Infragistics XamDataGrid, ofcourse this functionality may work differently in other versions of the control.<\/em><\/p>\n<p>I&#8217;m still getting to grips with the Infragistics XamDataGrid. Whilst I know the old UltraGrid for WinForms pretty well, this is worthless experience when using the WPF XamDataGrid.<\/p>\n<p>I was working on a way to highlight a &#8220;Reference&#8221; field\/column in the XamDataGrid and got this working nicely using a Converter with a CellValuePresenterStyle but whilst playing with the UI I notice that horizontally scrolling the grid demonstrated a very strange behaviour. Initially my reference column was displayed with a gray background (my chosen colour) but when I scroll it out of view and back into view, it turned white (the default background). Worse still another column went gray. Not the sort of functionality a customer\/user would want to see !<\/p>\n<p>Let&#8217;s take a look at an example. <\/p>\n<p><em>Note: this is a contrived example to demonstrate the issue, the code I was working on was more dynamic and the fields set-up in code behind, but I wanted to distill the example into it&#8217;s simplest components.<\/em><\/p>\n<p>In this sample code I&#8217;m using a DataTable and a CellValuePresenterStyle (obviously this problem may not occur in different scenarios).<\/p>\n<p>First up, let&#8217;s look at the sample data, we&#8217;re going to create enough columns to require us to horizontally scroll<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nvar dataTable = new DataTable();\r\ndataTable.Columns.Add(&quot;A1&quot;, typeof(int));\r\ndataTable.Columns.Add(&quot;B1&quot;, typeof(int));\r\ndataTable.Columns.Add(&quot;C1&quot;, typeof(int));\r\ndataTable.Columns.Add(&quot;D1&quot;, typeof(int));\r\ndataTable.Columns.Add(&quot;E1&quot;, typeof(int));\r\ndataTable.Columns.Add(&quot;F1&quot;, typeof(int));\r\ndataTable.Columns.Add(&quot;G1&quot;, typeof(int));\r\ndataTable.Columns.Add(&quot;H1&quot;, typeof(int));\r\ndataTable.Columns.Add(&quot;I1&quot;, typeof(int));\r\ndataTable.Columns.Add(&quot;J1&quot;, typeof(int));\r\ndataTable.Columns.Add(&quot;K1&quot;, typeof(int));\r\ndataTable.Columns.Add(&quot;L1&quot;, typeof(int));\r\ndataTable.Columns.Add(&quot;M1&quot;, typeof(int));\r\ndataTable.Columns.Add(&quot;N1&quot;, typeof(int));\r\n\r\nvar row1 = dataTable.NewRow();\r\nrow1&#x5B;&quot;A1&quot;] = 1;\r\nrow1&#x5B;&quot;B1&quot;] = 1;\r\nrow1&#x5B;&quot;C1&quot;] = 1;\r\nrow1&#x5B;&quot;D1&quot;] = 1;\r\nrow1&#x5B;&quot;E1&quot;] = 1;\r\nrow1&#x5B;&quot;F1&quot;] = 1;\r\nrow1&#x5B;&quot;G1&quot;] = 1;\r\nrow1&#x5B;&quot;H1&quot;] = 1;\r\nrow1&#x5B;&quot;I1&quot;] = 1;\r\nrow1&#x5B;&quot;J1&quot;] = 1;\r\nrow1&#x5B;&quot;K1&quot;] = 1;\r\nrow1&#x5B;&quot;L1&quot;] = 1;\r\nrow1&#x5B;&quot;M1&quot;] = 1;\r\nrow1&#x5B;&quot;N1&quot;] = 1;\r\ndataTable.Rows.Add(row1);\r\n\r\nvar row2 = dataTable.NewRow();\r\nrow2&#x5B;&quot;A1&quot;] = 2;\r\nrow2&#x5B;&quot;B1&quot;] = 2;\r\nrow2&#x5B;&quot;C1&quot;] = 2;\r\nrow2&#x5B;&quot;D1&quot;] = 2;\r\nrow2&#x5B;&quot;E1&quot;] = 2;\r\nrow2&#x5B;&quot;F1&quot;] = 2;\r\nrow2&#x5B;&quot;G1&quot;] = 2;\r\nrow2&#x5B;&quot;H1&quot;] = 2;\r\nrow2&#x5B;&quot;I1&quot;] = 2;\r\nrow2&#x5B;&quot;J1&quot;] = 2;\r\nrow2&#x5B;&quot;K1&quot;] = 2;\r\nrow2&#x5B;&quot;L1&quot;] = 2;\r\nrow2&#x5B;&quot;M1&quot;] = 2;\r\nrow2&#x5B;&quot;N1&quot;] = 2;\r\ndataTable.Rows.Add(row2);\r\n\r\nDataContext = dataTable;\r\n<\/pre>\n<p>Next up, let&#8217;s look at the XAML for the XamDataGrid<\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\n&lt;igDP:XamDataGrid DataSource=&quot;{Binding}&quot; &gt;\r\n   &lt;igDP:XamDataGrid.FieldLayouts&gt;\r\n      &lt;igDP:FieldLayout&gt;\r\n         &lt;igDP:FieldLayout.Fields&gt;\r\n            &lt;igDP:Field Name=&quot;A1&quot; Label=&quot;A1&quot;&gt;\r\n               &lt;igDP:Field.Settings&gt;\r\n                  &lt;igDP:FieldSettings CellValuePresenterStyle=&quot;{StaticResource CellValuePresenterStyle}&quot;\/&gt;\r\n               &lt;\/igDP:Field.Settings&gt;\r\n            &lt;\/igDP:Field&gt;\r\n            &lt;igDP:Field Name=&quot;B1&quot; Label=&quot;B1&quot;&gt;\r\n               &lt;igDP:Field.Settings&gt;\r\n                  &lt;igDP:FieldSettings CellValuePresenterStyle=&quot;{StaticResource CellValuePresenterStyle}&quot;\/&gt;\r\n               &lt;\/igDP:Field.Settings&gt;\r\n            &lt;\/igDP:Field&gt;\r\n            &lt;igDP:Field Name=&quot;B1&quot; Label=&quot;B1&quot;&gt;\r\n               &lt;igDP:Field.Settings&gt;\r\n                  &lt;igDP:FieldSettings CellValuePresenterStyle=&quot;{StaticResource CellValuePresenterStyle}&quot;\/&gt;\r\n               &lt;\/igDP:Field.Settings&gt;\r\n            &lt;\/igDP:Field&gt;\r\n            &lt;igDP:Field Name=&quot;C1&quot; Label=&quot;C1&quot;&gt;\r\n               &lt;igDP:Field.Settings&gt;\r\n                  &lt;igDP:FieldSettings CellValuePresenterStyle=&quot;{StaticResource CellValuePresenterStyle}&quot;\/&gt;\r\n               &lt;\/igDP:Field.Settings&gt;\r\n            &lt;\/igDP:Field&gt;\r\n            &lt;igDP:Field Name=&quot;D1&quot; Label=&quot;D1&quot;&gt;\r\n               &lt;igDP:Field.Settings&gt;\r\n                  &lt;igDP:FieldSettings CellValuePresenterStyle=&quot;{StaticResource CellValuePresenterStyle}&quot;\/&gt;\r\n               &lt;\/igDP:Field.Settings&gt;\r\n            &lt;\/igDP:Field&gt;\r\n            &lt;igDP:Field Name=&quot;E1&quot; Label=&quot;E1&quot;&gt;\r\n               &lt;igDP:Field.Settings&gt;\r\n                  &lt;igDP:FieldSettings CellValuePresenterStyle=&quot;{StaticResource CellValuePresenterStyle}&quot;\/&gt;\r\n               &lt;\/igDP:Field.Settings&gt;\r\n            &lt;\/igDP:Field&gt;\r\n            &lt;igDP:Field Name=&quot;F1&quot; Label=&quot;F1&quot;&gt;\r\n               &lt;igDP:Field.Settings&gt;\r\n                  &lt;igDP:FieldSettings CellValuePresenterStyle=&quot;{StaticResource CellValuePresenterStyle}&quot;\/&gt;\r\n               &lt;\/igDP:Field.Settings&gt;\r\n            &lt;\/igDP:Field&gt;\r\n            &lt;igDP:Field Name=&quot;G1&quot; Label=&quot;G1&quot;&gt;\r\n               &lt;igDP:Field.Settings&gt;\r\n                  &lt;igDP:FieldSettings CellValuePresenterStyle=&quot;{StaticResource CellValuePresenterStyle}&quot;\/&gt;\r\n               &lt;\/igDP:Field.Settings&gt;\r\n            &lt;\/igDP:Field&gt;\r\n            &lt;igDP:Field Name=&quot;G1&quot; Label=&quot;G1&quot;&gt;\r\n               &lt;igDP:Field.Settings&gt;\r\n                  &lt;igDP:FieldSettings CellValuePresenterStyle=&quot;{StaticResource CellValuePresenterStyle}&quot;\/&gt;\r\n               &lt;\/igDP:Field.Settings&gt;\r\n            &lt;\/igDP:Field&gt;\r\n            &lt;igDP:Field Name=&quot;H1&quot; Label=&quot;H1&quot;&gt;\r\n               &lt;igDP:Field.Settings&gt;\r\n                  &lt;igDP:FieldSettings CellValuePresenterStyle=&quot;{StaticResource CellValuePresenterStyle}&quot;\/&gt;\r\n               &lt;\/igDP:Field.Settings&gt;\r\n            &lt;\/igDP:Field&gt;\r\n            &lt;igDP:Field Name=&quot;I1&quot; Label=&quot;I1&quot;&gt;\r\n               &lt;igDP:Field.Settings&gt;\r\n                  &lt;igDP:FieldSettings CellValuePresenterStyle=&quot;{StaticResource CellValuePresenterStyle}&quot;\/&gt;\r\n               &lt;\/igDP:Field.Settings&gt;\r\n            &lt;\/igDP:Field&gt;\r\n            &lt;igDP:Field Name=&quot;J1&quot; Label=&quot;J1&quot;&gt;\r\n               &lt;igDP:Field.Settings&gt;\r\n                  &lt;igDP:FieldSettings CellValuePresenterStyle=&quot;{StaticResource CellValuePresenterStyle}&quot;\/&gt;\r\n               &lt;\/igDP:Field.Settings&gt;\r\n            &lt;\/igDP:Field&gt;\r\n            &lt;igDP:Field Name=&quot;K1&quot; Label=&quot;K1&quot;&gt;\r\n               &lt;igDP:Field.Settings&gt;\r\n                  &lt;igDP:FieldSettings CellValuePresenterStyle=&quot;{StaticResource CellValuePresenterStyle}&quot;\/&gt;\r\n               &lt;\/igDP:Field.Settings&gt;\r\n            &lt;\/igDP:Field&gt;\r\n            &lt;igDP:Field Name=&quot;L1&quot; Label=&quot;L1&quot;&gt;\r\n               &lt;igDP:Field.Settings&gt;\r\n                  &lt;igDP:FieldSettings CellValuePresenterStyle=&quot;{StaticResource CellValuePresenterStyle}&quot;\/&gt;\r\n               &lt;\/igDP:Field.Settings&gt;\r\n            &lt;\/igDP:Field&gt;\r\n            &lt;igDP:Field Name=&quot;M1&quot; Label=&quot;M1&quot;&gt;\r\n               &lt;igDP:Field.Settings&gt;\r\n                  &lt;igDP:FieldSettings CellValuePresenterStyle=&quot;{StaticResource CellValuePresenterStyle}&quot;\/&gt;\r\n               &lt;\/igDP:Field.Settings&gt;\r\n            &lt;\/igDP:Field&gt;\r\n            &lt;igDP:Field Name=&quot;N1&quot; Label=&quot;N1&quot;&gt;\r\n               &lt;igDP:Field.Settings&gt;\r\n                  &lt;igDP:FieldSettings CellValuePresenterStyle=&quot;{StaticResource CellValuePresenterStyle}&quot;\/&gt;\r\n               &lt;\/igDP:Field.Settings&gt;\r\n            &lt;\/igDP:Field&gt;\r\n         &lt;\/igDP:FieldLayout.Fields&gt;\r\n      &lt;\/igDP:FieldLayout&gt;\r\n   &lt;\/igDP:XamDataGrid.FieldLayouts&gt;\r\n&lt;\/igDP:XamDataGrid&gt;\r\n<\/pre>\n<p>Next let&#8217;s look at style<\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\n&lt;Style TargetType=&quot;{x:Type igDP:CellValuePresenter}&quot; x:Key=&quot;CellValuePresenterStyle&quot;&gt;\r\n   &lt;Setter Property=&quot;Background&quot; \r\n        Value=&quot;{Binding RelativeSource={RelativeSource Self},  \r\n        Converter={StaticResource BackgroundConverter}}&quot; \/&gt;\r\n&lt;\/Style&gt;\r\n<\/pre>\n<p>and finally the converter<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\npublic class BackgroundConverter : IValueConverter\r\n{\r\n   public object Convert(object value, Type targetType, object parameter, CultureInfo culture)\r\n   {\r\n      var cellValuePresenter = value as CellValuePresenter;\r\n      if (cellValuePresenter != null)\r\n      {\r\n         \/\/ in my code this is &quot;discovered&quot; at runtime, but you get the idea\r\n         if (cellValuePresenter.Field.Name == &quot;E1&quot;)\r\n            return Brushes.Gray;\r\n      }\r\n\r\n      return Binding.DoNothing;\r\n   }\r\n\r\n   public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)\r\n   {\r\n      throw new NotImplementedException();\r\n   }\r\n}\r\n<\/pre>\n<p>Now if you run up an example WPF Window with these parts and horizontally scroll you should see the gray background column switch from the E1 column to other columns.<\/p>\n<p><strong>A solution<\/strong><\/p>\n<p>So I had a chat with a colleague who mentioned the grid&#8217;s virtualization options being a possible issue &#8211; lo and behold adding the <strong>CellContainerGenerationMode<\/strong> attribute and setting it to <strong>LazyLoad<\/strong> worked.<\/p>\n<p>For example<\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\n&lt;igDP:XamDataGrid DataSource=&quot;{Binding DefaultView}&quot; CellContainerGenerationMode=&quot;LazyLoad&quot;&gt;\r\n<\/pre>\n<p>It appears the XamDataGrid has a default value of Recycle for the CellContainerGenerationMode and an educated guess (based as much on the naming as anything) suggests this means the XamDataGrid is reusing the field\/column when scrolling &#8211; this is all well and good when your column styling is static, but not so good when you have something a little more dynamic. <\/p>\n<p><strong>References<\/strong><\/p>\n<p>Although not talking about this specific issue, it&#8217;s worth noting this post of performance and optimization within the XamDataGrid &#8211; <a href=\"http:\/\/www.infragistics.com\/community\/blogs\/kiril_matev\/archive\/2010\/10\/26\/optimizing-xamdatagrid-performance.aspx\" title=\"Optimizing Infragistics XamDataGrid Performance\" target=\"_blank\">Optimizing Infragistics XamDataGrid Performance<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The column background colour in my XamDataGrid keeps changing or to put it another way, beware the CellContainerGenerationMode when overriding the CellValuePresenterStyle. What&#8217;s the problem ? First off, please note I&#8217;m using v12.2 of Infragistics XamDataGrid, ofcourse this functionality may work differently in other versions of the control. I&#8217;m still getting to grips with the [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[85],"tags":[],"class_list":["post-2971","post","type-post","status-publish","format-standard","hentry","category-xamdatagrid"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/2971","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/comments?post=2971"}],"version-history":[{"count":10,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/2971\/revisions"}],"predecessor-version":[{"id":3003,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/2971\/revisions\/3003"}],"wp:attachment":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/media?parent=2971"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/categories?post=2971"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/tags?post=2971"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}