I have noticed some interest in my earlier post on how to add a WPF data template to a resource dictionary in code and so this post is a demonstration of the technique. The code is presented in F# but I have deliberately kept the code simple and fairly non-functional so that those of a C# or VB.NET background can follow the process easily.
Following on from my last post, and in preparation for a series on producing a full WPF application in F#, in this post I discuss the differences between the |> and >> operators and some additional functions I find helpful for composing functions.
The first question that any functional programmer should ask is, “why are there side effects?!” Side effects are anathema to functional programming purists because they introduce many kinds of undesirable characteristics into code, limiting optimisation and restructuring options. However, most object-oriented libraries, including the .NET BCL, depend on side effects and many of these are intrinsic to writing .NET applications.
WPF is a special case where this applies, with DependencyProperty fields.
If you need (or want) to add a DataTemplate (or related class) object to a WPF resource dictionary programmatically then you need to understand how XAML data templates are added to the resource dictionary.
The problem is a simple one: add a toolbar image into an assembly and make it available as a resource in WPF, but use only XAML and F#.
Another quick post to demonstrate how to create WPF Command classes in F#. Notably, these include a CLR event in the inherited interface that needs to be implemented in F#.
This is a quick post demonstrating how to create WPF Dependency Properties in F#. There is a bit of a trick to it as you have to use the val keyword to define a field and that field must be mutable and you also have to use explicit interface access.
I have been monitoring the hits on my blog and there seems to be a fair bit of interest in the Microsoft GLEE library. Unfortunately, I have found it to be fairly incomplete for many interesting scenarios. In the process of this mini-investigation I developed a small utility to allow the manual construction of graphs. You can find both the code and the application below. You can also save out your graphs as images (but not, sadly, in a serialized form).
For those of you that do not have Microsoft Expression Blend (or an equivalent), using XAMLPad and Visual Studio can be okay. However, if you want to create custom controls that are similar to existing (standard or third party) controls then it can be tricky to find out how best to do it. This little utility uses the XamlWriter class to export a default template from a WPF control.
I am currently working on a project where I want to decouple command processing from command invocation. This is usually done using something like the Command pattern, however that pattern does not lend itself to functional composition of commands. What I wanted was something more akin to the processing model in the Emacs editor.