Convention to bind multiple properties of control to viewmodel exposed as property

Topics: Conventions
Sep 20, 2011 at 6:26 AM

I'm trying to do this with RadBusyIndicator, Telerik's version of BusyIndicator.

Without using Caliburn Micro's conventions, the following XAML binds properly.  "BusyIndicator" is a property on the ShellViewModel that exposes a RadBusyIndicatorViewModel.


<tel:RadBusyIndicator IsBusy="{Binding BusyIndicator.IsBusy}" BusyContent="{Binding BusyIndicator.BusyContent}">




Valeriu Caraulean's Telerik conventions on GitHub binds "IsBusy" only.  It would be nice to be able to set up a CM convention for RadBusyIndicator that would bind multiple properties to an appropriate viewmodel.  This would also apply to other third-party controls and UserControls.  I'm sure this is something that's done often, but I couldn't find a good example. 

I'd be grateful if someone would point me in the right direction.

 -- Vikram

Sep 20, 2011 at 8:36 PM

It should be something along this line:

ConventionManager.AddElementConvention<RadBusyIndicator>(RadBusyIndicator.IsBusyProperty, "IsBusy", "Loaded")
	.ApplyBinding = (viewModelType, path, property, element, convention) =>
		//Default binding to "IsBusy" property
		if (!ConventionManager.SetBinding(viewModelType, path + ".IsBusy", property, element, convention))
			 return false;

		//Optional binding to "BusyContent" property
		if(!HasBinding(element, RadBusyIndicator.BusyContentProperty))
			var binding = new Binding(path + ".BusyContent");
			ApplyValueConverter(binding, bindableProperty, property);
			ApplyStringFormat(binding, convention, property);

			BindingOperations.SetBinding(element, RadBusyIndicator.BusyContentProperty, binding);
		return true;

Sep 21, 2011 at 6:06 AM

Thanks, works perfectly with one minor tweak:

ConventionManager.ApplyValueConverter( binding, RadBusyIndicator.BusyContentProperty, property );
-- Vikram