{"id":3005,"date":"2022-10-11T20:14:19","date_gmt":"2022-10-11T20:14:19","guid":{"rendered":"http:\/\/putridparrot.com\/blog\/?p=3005"},"modified":"2022-10-11T20:14:19","modified_gmt":"2022-10-11T20:14:19","slug":"xamdatagrid-tips-and-tricks","status":"publish","type":"post","link":"https:\/\/putridparrot.com\/blog\/xamdatagrid-tips-and-tricks\/","title":{"rendered":"XamDataGrid tips and tricks"},"content":{"rendered":"<p><em>Note: This post was written a while back but sat in draft. I&#8217;ve published this now, but I&#8217;m not sure it&#8217;s relevant to the latest versions etc. so please bear this in mind.<\/em><\/p>\n<p>The following is a general list of tips and tricks for working with the XamDataGrid. Primarily code\/xaml which doesn&#8217;t require a full post of their own.<\/p>\n<p><strong>Enable\/disable column sorting or change the way the sorting is applied<\/strong><\/p>\n<p>Simply set the LabelClickAction on the FieldSettings to &#8220;Nothing&#8221; to turn sorting off. You can also change the logic for sort in that you can set the sorting to single or multi field.<\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\n&lt;igDP:XamDataGrid&gt;\r\n   &lt;igDP:XamDataGrid.FieldSettings&gt;\r\n      &lt;igDP:FieldSettings LabelClickAction=&quot;Nothing&quot; \/&gt;\r\n   &lt;\/igDP:XamDataGrid.FieldSettings&gt;\r\n&lt;\/igDP:XamDataGrid&gt;\r\n<\/pre>\n<p><strong>Changing cell styles and more in code-behind<\/strong><\/p>\n<p>Sometimes our requirement is too dynamic for the declarative nature of XAML and\/or may be difficult to handle via styles\/triggers and converters so we need to write some code. The problem is getting at the cell object at the right time.<\/p>\n<p>This is where the CellsInViewChanged event is useful, for example<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nprivate void CellsInViewChanged(object sender, CellsInViewChangedEventArgs e)\r\n{\r\n   foreach(var visibleDataBlock in e.CurrentCellsInView)\r\n   {\r\n      foreach(var record in visibleDataBlock.Records)\r\n      {\r\n         foreach(var field in visibleDataBlock.Fields)\r\n         {\r\n            var cellValuePresenter = CellValuePresenter.FromRecordAndField(record, field);\r\n            \/\/ do something with the cell value presenter\r\n         }\r\n      }\r\n   }\r\n}\r\n<\/pre>\n<p>In the above code (which we seem to have to attach an event handler to, i.e. there&#8217;s no virtual method for deriving our own implementation from that I can see). We get a list of VisibleDataBlock items which we loop through to get the records and the fields. Using the CellValuePresenter.FromRecordAndField we can then get the CellValuePresenter where we can then change visual style directly, such as the cell background colour, we can make the cell&#8217;s editor readonly and so on.<\/p>\n<p><strong>How to stop a cell &#8220;appearing&#8221; to go into edit mode<\/strong><\/p>\n<p>We may set a cell&#8217;s editor to readonly but when the user clicks on the cell the cell changes colour as if switching to edit more and the caret is displayed, even though you cannot edit the field. A way to stop this happening is by overriding the OnEditModeStarting method in a subclassed grid control or intercepting the EditModeStarting event on the grid. For example<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nprotected override void OnEditModeStarting(EditModeStartingEventArgs args)\r\n{\r\n   \/\/ required to stop the cell even entering edit mode\r\n   var cvp = CellValuePresenter.FromCell(args.Cell);\r\n   if (cvp != null)\r\n   {\r\n      args.Handled = args.Cancel = cvp.Editor.IsReadOnly;\r\n   }\r\n}\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Note: This post was written a while back but sat in draft. I&#8217;ve published this now, but I&#8217;m not sure it&#8217;s relevant to the latest versions etc. so please bear this in mind. The following is a general list of tips and tricks for working with the XamDataGrid. Primarily code\/xaml which doesn&#8217;t require a full [&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-3005","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\/3005","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=3005"}],"version-history":[{"count":5,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/3005\/revisions"}],"predecessor-version":[{"id":9580,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/3005\/revisions\/9580"}],"wp:attachment":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/media?parent=3005"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/categories?post=3005"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/tags?post=3005"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}