Binding Property Objects to WPF with Caliburn.Micro

Caliburn.Micro provides some powerful classes to help when working in WPF and I found it a very useful and rapid way to develop a WPF application for a client.

However, I found myself reproducing the same binding properties over and over (for example Foreground and Background colours for Labels, Buttons, TextBoxes, etc.). I thought that there must be a better way of handling the common properties for all these objects – rather than multiple Properties essentially doing the same thing. It seemed unlikely that there wasn’t a way of creating a class that could contain all the properties and be used to bind to the XAML objects.

The solution was suggested in this article by Colin Eberhardt.

Based on this I created the following class in the Models section of the project:

public class CommonControlProperties : PropertyChangedBase
    public CommonControlProperties()
        : this(new SolidColorBrush(Colors.Black), new SolidColorBrush(Colors.White))
    public CommonControlProperties(SolidColorBrush foreground, SolidColorBrush background)
        Foreground = foreground;
        Background = background;
    private SolidColorBrush _background;
    public SolidColorBrush Background
        get { return _background; }
            _background = value;
            NotifyOfPropertyChange(() => Background);
    private SolidColorBrush _foreground;
    public SolidColorBrush Foreground
        get { return _foreground; }
            _foreground = value;
            NotifyOfPropertyChange(() => Foreground);

This inherits Caliburn.Micro‘s PropertyChangedBase which provides the NotifyOfPropertyChange method. It implements only 2 properties for this example: Foreground and Background colours. Two constructors are provided – the basic just sets foreground to black and background to white by default, the second uses two SolidColorBrush items to set the foreground and background colours.

In the ViewModel I then declared an auto implemented Property of type CommonControlProperties:

public CommonControlProperties TestTextBox { get; set; }

This can be instantiated in the ViewModel constructor (or elsewhere):

TestTextBox = new CommonControlProperties();
TestTextBox = new CommonControlProperties
    Background = new SolidColorBrush(Colors.Bisque),
    Foreground = new SolidColorBrush(Colors.Red)

Finally, the XAML code for the TextBox uses bindings to the ViewModel property and its relevant contained properties.

<TextBox x:Name="TestTextBox1" Background="{Binding TestTextBox.Background}" Foreground="{Binding TestTextBox.Foreground}" HorizontalContentAlignment="Left" Height="27" Margin="43,20,0,0" Grid.Row="3" TextWrapping="Wrap" Text="TextBox1" VerticalAlignment="Top" Width="132" FontSize="14" FontWeight="Bold"/>

This works as expected and allows a neat container for common properties that need to be bound to the XAML objects.

Obviously, this small class needs to be extended to contain all the properties that are useful for controlling the appearance of the visual Control.


2 thoughts on “Binding Property Objects to WPF with Caliburn.Micro

    • Hi Morten,

      Yes, firstly Caliburn.Micro is a very good way of both insulating yourself from the trickier/tedious bits of WPF and of working with multiple versions of Windows (works with desktop / web / winphone systems).

      The code I’ve shown in the post could be used with standard WPF (without Caliburn.Micro) but would probably need additional support (using INotifyPropertyChanged, I think).

      I have now considerably extended the code and generalised some parts – for example the SolidColorBrush items are now Brush to allow use of any Brush type.
      It allows me (behind the scenes) to create / initialise / change the state of collections of buttons, labels, text boxes etc.. So, for example, I can change the default Button very easily depending on the software state. I can also initialise the entire collection to the same inital state if need be.
      It also now has ToolTip text as a property – so that can be bound to a Button, TextBox or label if needed (so it allows use from the Resources in the software)

      A colleague (Barry Carr) had commented originally that this could / may be done using Templates in WPF – but I’m not sure how flexible that approach is. The approach I have used works for me in this case. I’ll post later when I’ve worked some more with it.

Leave a Reply

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

You are commenting using your 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 )

Connecting to %s