Binding to non-viewmodel data

Jan 28, 2011 at 9:48 PM

I'm exploring using Caliburn Micro conventions for localization. The idea would be to use a custom convention bind Label (and other similar) controls to resources directly (typically localization is done by binding through a static resource that exposes the resource strings). However, I'm stymied by the fact that CM seems to always want to bind to the ViewModel - if there's no property on the VM that matches the control name, the convention doesn't get applied (I'm looking at the BindProperties() method on ViewModelBinder). 

Am I misunderstanding something here? Is there a way to apply a convention that doesn't directly involve a corresponding property on a viewmodel? I suppose I could expose the resources through the viewmodel, but that seems a bit kludgey. 

Jan 29, 2011 at 9:17 AM

I agree that labels L10n concerns should not poison the ViewModel (even though in some circumstances VM may have to deal with localized strings/messages directly...)
However, since the binding of labels against the VM is *very* frequent, too, the binding has no general clue to determine other potential binding sources. 

Yet, you might introduce an app-specific convention, using a special name prefix for labels that should only be localized.
To accomplish this, you might customize the BindProperties method in order to attempt a bind against one or more resource class if the name of the element has the "special" prefix.


Jan 29, 2011 at 11:27 AM

I read quite a lot of articles regarding localization, and I must say there is no deifnive spproach.

I prefer a mixed approach:

  • use view-model properties to store localized resources that can change dynamically and are part of the view logic (e.g. status messages, message box messages, window titles...)
  • use a specialized localization markup extension to retrieve localized data in the view for 'decorative' resources (e.g. static labels, warning messages, disclaimers...)

That said, it is of course possible to bind to non-view-model data customizing BindProperties, as marco said. You just need to define your own convention approach (e.g. identfy all localizable controls by name and determine the localized data using the name itself as a key).

Jan 29, 2011 at 1:50 PM

What if I added a new func to the ViewModelBinder that was called with a list of all non-matched UI elements. The default behavior would basically do nothing. But it would be an extension point for adding this type of custom behavior without changing a lot of internals. Thoughts?

Jan 29, 2011 at 3:30 PM

It would be optimal, so you don't have to copy and tweak the existing implementation.

Jan 29, 2011 at 3:34 PM

It could be nice to have a default extension point to add custom behaviours, but I suppose you should provide even a matching function (that by default returns always true), used to determine if a control should be processed by the default binding behaviour or not. This would allow special scenarios where the name is not the only criteria to filter out the behaviour.

Jan 29, 2011 at 4:24 PM

I'll be thinking about this and try to come up with something by Monday.

Jan 30, 2011 at 12:43 AM

I added a hook as of revision 825e62f45cbe The commit notes should explain. Let me know if that will work for you.

Jan 31, 2011 at 6:29 PM

Rob, you completely and totally rock. I'll take it for a spin and report back.