Caliburn Micro: how to set UpdateSourceTrigger binding?

I studied the Caliburn Micro MVVM Framework to understand it, but I ran into some problem. I have a TextBox associated with a string property in my ViewModel, and I would like the property to be updated when the TextBox loses focus.

Normally, I would do this by setting UpdateSourceTrigger in LostFocus to a binding, but I see no way to do this in Caliburn, since it automatically set the property binding for me. Currently, the property is updated every time the contents of the TextBox changes.

My code is very simple, for example, here is my virtual machine:

public class ShellViewModel : PropertyChangeBase { private string _name; public string Name { get { return _name; } set { _name = value; NotifyOfPropertyChange(() => Name); } } } 

And inside my view, I have a simple TextBox.

 <TextBox x:Name="Name" /> 

How can I change it so that the Name property is updated only when the TextBox loses focus, and not every time the property changes?

+7
source share
1 answer

Just set the binding for this TextBox instance and Caliburn.Micro will not touch it:

 <TextBox Text="{Binding Name, UpdateSourceTrigger=LostFocus}" /> 

Alternatively, if you want to change the default behavior for all TextBox instances, you can change the implementation of the ConventionManager.ApplyUpdateSourceTrigger in the bootstrapper Configure method.

Something like:

 protected override void Configure() { ConventionManager.ApplyUpdateSourceTrigger = (bindableProperty, element, binding) =>{ #if SILVERLIGHT ApplySilverlightTriggers( element, bindableProperty, x => x.GetBindingExpression(bindableProperty), info, binding ); #else if (element is TextBox) { return; } binding.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged; #endif }; } 
+21
source

All Articles