{"id":6280,"date":"2018-06-16T20:49:19","date_gmt":"2018-06-16T20:49:19","guid":{"rendered":"http:\/\/putridparrot.com\/blog\/?p=6280"},"modified":"2018-06-16T20:49:20","modified_gmt":"2018-06-16T20:49:20","slug":"turning-xaml-into-objects-using-xamlreader","status":"publish","type":"post","link":"https:\/\/putridparrot.com\/blog\/turning-xaml-into-objects-using-xamlreader\/","title":{"rendered":"Turning XAML into objects using XamlReader"},"content":{"rendered":"<p>I couldn&#8217;t come up with a good title for this post, basically the post is about taking a XAML declared Style and finding the easiest way to turn this into a Style object in code.<\/p>\n<p>I had a problem whereby I ended up with multiple style objects which were exactly the same except for the DataItem they were binding to. This ended up meaning I had lots of duplicate XAML code, apart from one difference. Once I started making the code more dynamic (it was adding columns to the Infragistics XamDataGrid) this XAML became almost useless as I needed to create a style for each dynamically added column and hence really need to generate the Style in code.<\/p>\n<p>If you&#8217;ve done anything much with code behind for such things as creating Style objects or replicating XAML code you&#8217;ve probably found the code doesn&#8217;t always map directly to the XAML, and if you already have XAML that works, why not simply reused it. <\/p>\n<p><em>Note: The reason the code doesn&#8217;t always map is that it includes resources, converters and other XAML magically things that are automatically applied, for example converting colour names to colour values, or field lengths etc.<\/em> <\/p>\n<p>So let&#8217;s simple copy our XAML into a string in code and then programmatically change the DataItem field and then generate our Style object from this. <\/p>\n<p>Here&#8217;s an example of the XAML as a string<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nvar styleString = \r\n   &quot;&lt;Style TargetType=\\&quot;{x:Type idp:CellValuePresenter}\\&quot;&gt;&quot; +\r\n   &quot;&lt;Setter Property=\\&quot;Template\\&quot;&gt;&quot; +\r\n   &quot;&lt;Setter.Value&gt;&quot; +\r\n   &quot;&lt;ControlTemplate&gt;&quot; +\r\n   &quot;&lt;Grid&gt;&quot; +\r\n   &quot;&lt;ListBox ItemsSource=\\&quot;{Binding DataItem.&quot; + source + &quot;}\\&quot;&gt;&quot; +\r\n   &quot;&lt;ListBox.ItemTemplate&gt;&quot; +\r\n   &quot;&lt;DataTemplate&gt;&quot; +\r\n   &quot;&lt;TextBlock HorizontalAlignment=\\&quot;Stretch\\&quot; Text=\\&quot;{Binding}\\&quot;\/&gt;&quot; +\r\n   &quot;&lt;\/DataTemplate&gt;&quot; +\r\n   &quot;&lt;\/ListBox.ItemTemplate&gt;&quot; +\r\n   &quot;&lt;ListBox.ItemsPanel&gt;&quot; +\r\n   &quot;&lt;ItemsPanelTemplate&gt;&quot; +\r\n   &quot;&lt;StackPanel Orientation=\\&quot;Vertical\\&quot;\/&gt;&quot; +\r\n   &quot;&lt;\/ItemsPanelTemplate&gt;&quot; +\r\n   &quot;&lt;\/ListBox.ItemsPanel&gt;&quot; +\r\n   &quot;&lt;\/ListBox&gt;&quot; +\r\n   &quot;&lt;\/Grid&gt;&quot; +\r\n   &quot;&lt;\/ControlTemplate&gt;&quot; +\r\n   &quot;&lt;\/Setter.Value&gt;&quot; +\r\n   &quot;&lt;\/Setter&gt;&quot; +\r\n   &quot;&lt;\/Style&gt;&quot;;\r\n<\/pre>\n<p>In the above we&#8217;d obviously supply the <em>source<\/em> variable and this will then form part of our XAML style. To turn this into a Style object we need to use the <em>XamlReader.Parse<\/em> method. Before we can use the <em>XamlReader.Parse<\/em> method we also need to create the namespaces (context items) that we would expect from our XAML code, in this case we create a <em>ParseContent<\/em> object and assign our namespaces, then pass this into the <em>XamlReader.Parse<\/em> method also with the styleString. Our code might look like the following<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\npublic Style CreateStyle(string source)\r\n{\r\n   var context = new ParserContext();\r\n   context.XmlnsDictionary.Add\r\n   (&quot;&quot;, \r\n    &quot;http:\/\/schemas.microsoft.com\/winfx\/2006\/xaml\/presentation&quot;);\r\n   context.XmlnsDictionary.Add\r\n   (&quot;x&quot;, \r\n    &quot;http:\/\/schemas.microsoft.com\/winfx\/2006\/xaml&quot;);\r\n\r\n   \/\/ other namespaces for XamaDataGrid etc.\r\n\r\n   var styleString = \r\n      &quot;&lt;Style TargetType=\\&quot;{x:Type dataPresenter:CellValuePresenter}\\&quot;&gt;&quot; +\r\n      &quot;&lt;Setter Property=\\&quot;Template\\&quot;&gt;&quot; +\r\n      &quot;&lt;Setter.Value&gt;&quot; +\r\n      &quot;&lt;ControlTemplate&gt;&quot; +\r\n      &quot;&lt;Grid&gt;&quot; +\r\n      &quot;&lt;ListBox ItemsSource=\\&quot;{Binding DataItem.&quot; + source + &quot;}\\&quot;&gt;&quot; +\r\n      &quot;&lt;ListBox.ItemTemplate&gt;&quot; +\r\n      &quot;&lt;DataTemplate&gt;&quot; +\r\n      &quot;&lt;TextBlock HorizontalAlignment=\\&quot;Stretch\\&quot; Text=\\&quot;{Binding}\\&quot;\/&gt;&quot; +\r\n      &quot;&lt;\/DataTemplate&gt;&quot; +\r\n      &quot;&lt;\/ListBox.ItemTemplate&gt;&quot; +\r\n      &quot;&lt;ListBox.ItemsPanel&gt;&quot; +\r\n      &quot;&lt;ItemsPanelTemplate&gt;&quot; +\r\n      &quot;&lt;StackPanel Orientation=\\&quot;Vertical\\&quot;\/&gt;&quot; +\r\n      &quot;&lt;\/ItemsPanelTemplate&gt;&quot; +\r\n      &quot;&lt;\/ListBox.ItemsPanel&gt;&quot; +\r\n      &quot;&lt;\/ListBox&gt;&quot; +\r\n      &quot;&lt;\/Grid&gt;&quot; +\r\n      &quot;&lt;\/ControlTemplate&gt;&quot; +\r\n      &quot;&lt;\/Setter.Value&gt;&quot; +\r\n      &quot;&lt;\/Setter&gt;&quot; +\r\n      &quot;&lt;\/Style&gt;&quot;;\r\n\r\n   return (Style)XamlReader.Parse(styleString, context);\r\n}\r\n<\/pre>\n<p>That&#8217;s it &#8211; much simpler that create each piece of the Style using C# objects and allows us to first test our XAML out then reused it to generate our objects in code.<\/p>\n<p><em>Note: If your code relied on namespaces\/assemblies which are not already loaded you will have to load them into memory yourself before they can be used in this way.<\/em><\/p>\n","protected":false},"excerpt":{"rendered":"<p>I couldn&#8217;t come up with a good title for this post, basically the post is about taking a XAML declared Style and finding the easiest way to turn this into a Style object in code. I had a problem whereby I ended up with multiple style objects which were exactly the same except for 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":[13,20],"tags":[],"class_list":["post-6280","post","type-post","status-publish","format-standard","hentry","category-wpf","category-xaml"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/6280","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=6280"}],"version-history":[{"count":4,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/6280\/revisions"}],"predecessor-version":[{"id":6294,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/6280\/revisions\/6294"}],"wp:attachment":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/media?parent=6280"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/categories?post=6280"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/tags?post=6280"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}