Microsoft XAML Toolkit CTP – July 2010: XAML DOM


This post describes the XAML DOM feature of the XAML Toolkit CTP – July 2010, announced here and downloadable here.  It borrows heavily from Mike Shim’s post announcing the first CTP.

Here’s a simple example of reading a XAML document and writing out all the types used:

By calling XamlDomServices.Load, we get a XamlDomObject for the root object in the XAML document.  From there, we can call DescendantsAndSelf (similar to XML XElement) on the root object which returns an IEnumerable<XamlDomObject>.

Here’s another example.  Imagine you want to set Background on every Control in your document that doesn’t already have one:

The call to DescendantsAndSelf this time is passed typeof(Control).  This will limit it to return only things that are assignable to Control.  We are leveraging LINQ to easily restrict the returned XamlDomObjects to only the ones without a “Background” member.  We then set the background property to Red with SetMemberValue("Background", "Red").  Finally, we call XamlDomServices.Save to save the file back out to XAML.  This is a pretty simple example of a transformation but we can do much more complex transformations with our XamlDom.



No Comments.