Phillip Trelford's Array

POKE 36879,255

F# operator overloads for WPF dependency properties

When creating a desktop application with WPF using F# there are a number of options:

For small desktop applications, the last option, creating WPF elements directly from F# can produce good self-contained code, but at time is a little less readable than the XAML equivalent. Lets consider placing a button bound to a command on a grid at a certain position.

The XAML fragment might look like this:

<Button Content="_1" Command="{Binding Key1Command}"
        Grid.Column="0" Grid.Row="2"/>


Code equivalent:

let button = Button(Content="_1")
button.SetBinding(Button.CommandProperty,Binding("Key1Command")) |> ignore


Code equivalent using (+) operator overloads to add the dependency properties:

Button(Content="_1") + Button.Command(Binding "Key1Command") 
    + Grid.Column 0 + Grid.Row 2 


The glue is just a couple of classes that define a dependency property and value/binding pair with a (+) operator overload that sets the value on the target WPF element.

For Dependency Property values:

type DependencyPropertyValuePair(dp:DependencyProperty,value:obj) =
    member this.Property = dp
    member this.Value = value
    static member (+) 
        (target:#UIElement,pair:DependencyPropertyValuePair) =


For Dependency Property bindings:

type DependencyPropertyBindingPair(dp:DependencyProperty,binding:BindingBase) =
    member this.Property = dp
    member this.Binding = binding
    static member (+) 
        (target:#FrameworkElement,pair:DependencyPropertyBindingPair) =
        target.SetBinding(pair.Property,pair.Binding) |> ignore


Finally relevant WPF types must be extended with helper methods (note: this could be code generated):

type Grid with
    static member Column (value:int) =
    static member Row (value:int) =


Attached is a sample calculator VS2010 project showing the mechanism in action.

Calculator (3.57 kb)

Mastermind F# WPF Game Sample

While playing around with the F# September CTP over the last week, I made a short, <300 lines, implementation of the Mastermind board game using some Windows Presentation Foundation (WPF) controls. I've been impressed with the integration of F# with Visual Studio 2008 with project references and property pages now in place. It also seems pretty easy to wire up a usable UI using the WPF controls event without XAML. Anyway you can view the source on the F# wiki or download it below and have a play.

Mastermind.fs (13.69 kb)