WPF / Silverlight DependencyProperty in F#

This is a quick post to share on how-to declare WPF / Silverlight DependencyProperty in F#. The reason, In F# there are no public static fields to declare. Read all about DPs (DependencyProperty in short) here. If you are developing a WPF / Silverlight solution libraries completely in F#, then you would have to declare DPs so you can expose the functionality to be used in any .NET language.

Let’s say we have an ItemsSource property to declare, below is the way it has to be declared,

    [<DefaultValue>]
    static val mutable private ItemsSourcePropertyInternal : DependencyProperty

    static member ItemsSourceProperty
        with get() =
            if CustomControl.ItemsSourcePropertyInternal = null then
                CustomControl.ItemsSourcePropertyInternal <- DependencyProperty.Register("ItemsSource", typeof<IEnumerable>, typeof<CustomControl>, new PropertyMetadata(null))
            CustomControl.ItemsSourcePropertyInternal

    member x.ItemsSource
        with get() = x.GetValue(CustomControl.ItemsSourceProperty) : ? > IEnumerable
        and set(v : IEnumerable) = x.SetValue(CustomControl.ItemsSourceProperty, v)

 

  1. Declare a private mutable variable that registers the DependencyProperty
  2. Write a static member variable with a getter that initializes the DP
  3. Declare the member property with a getter and setter.
    1. Important thing to note in the setter is to decorate the value “v” with the type. This is because of the fact that when this property gets exposed to other .NET languages like C# or VB, it causes errors assigning un-typed values to the property.

In WPF, there is a caveat. WPF can invoke private / internal field variables too from XAML, rather Silverlight can only invoke public variables from XAML. So we don’t require the notion of creating a private mutable static field (If we don’t wish to expose it). The same ItemsSource property can be declared as below In WPF,

    static let ItemsSourceProperty = DependencyProperty.Register("ItemsSource", typeof<IEnumerable>, typeof<SearchTwitterBox>, new PropertyMetadata(null))
    member x.ItemsSource 
        with get() = x.GetValue(ItemsSourceProperty) : ? > IEnumerable
        and set(v : IEnumerable) = x.SetValue(ItemsSourceProperty, v)

We won’t be able to expose the DP here, but it does work if you don’t want to expose the DP.

That’s it!

-Fahad

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: