F# - implementing an observable object

by Gregor Uhlenheuer on February 4, 2012

Lately I wanted to use a WPF ListBox with selectable items (with a DataTemplate and a simple CheckBox attached). In order to properly use such a thing you should implement the INotifyPropertyChanged interface 1. I hadn’t used events in F# before so had to experiment a few things to get it right.

The solution was to implement a CheckItem class that wraps the selection functionality and the implementation of the INotifyPropertyChanged interface. This is what it looks like:

There are a few things worth noting here: The first interesting thing is the attribute CLIEventAttribute 2 that allows us the use a more consise way of implementing events. Basically it adds the necessary CLI metadata to the event and implements the add_EventName and remove_EventName methods.

The second interesting fact is the usage of F# quotations to attach the right property name to the PropertyChangedEventArgs structure. Instead of specifying a string constant it is possible to use a property expression.


  1. INotifyPropertyChanged on msdn

  2. CLIEventAttribute on msdn

This post is tagged with f#, programming, .net and wpf