Compiled Experience

Adding functionality to Caliburn.Micro conventions

08 Jun 2017 by Nigel Sampson

The binding conventions in Caliburn.Micro can be extended to do almost anything you want. The method ConventionManager.AddElementConvention<T> returns a ElementConvention which can be further customised. In this post I’ll show what things we can do with this by modifying the ElementConvention.ApplyBinding action.

Internally Caliburn.Micro already modifies some of the conventions with extra functionality. It’s what turns

We can add our own extensions as well. In this example I’ll demonstrate how we can modify the convention for TextBox to take into account the System.ComponentModel.DataAnnotations attributes.

Let’s imagine we have a view model with FirstName property that looks like the one below, it would be great if we can by convention apply these attributes to the control the property is bound to. This should let us define in one place all the metadata about this property.

IF we try to apply the binding between the view model and the Text property on the control. If this fails (there’s already a binding) then we exit out. We then check the property for the attributes in question and if they exist apply their values to the TextBox.

We don’t need to make any modifications to the xaml so it can be very simple and still have most of the values it needs applied by the convention itself.

<TextBoxx:Name="FirstName"/>

Feel free to experiement in customising your conventions. I hope this gives you all some ideas on what you can do. Let me know what you find.