Caliburn Micro:如何设置绑定UpdateSourceTrigger?

Ale*_*ide 8 wpf mvvm caliburn.micro

我一直在探索Caliburn Micro MVVM框架只是为了感受它,但我遇到了一些问题.我有一个TextBox绑定到我的ViewModel上的字符串属性,我希望在TextBox失去焦点时更新属性.

通常我会通过在绑定上将UpdateSourceTrigger设置为LostFocus来实现这一点,但我没有看到任何方法在Caliburn中执行此操作,因为它已自动为我设置了属性绑定.目前,每次TextBox的内容更改时,都会更新该属性.

我的代码非常简单,例如这里是我的VM:

public class ShellViewModel : PropertyChangeBase
{
    private string _name;

    public string Name
    {
        get { return _name; }
        set 
        { 
            _name = value; 
            NotifyOfPropertyChange(() => Name);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

在我的视图中,我有一个简单的TextBox.

<TextBox x:Name="Name" />
Run Code Online (Sandbox Code Playgroud)

如何更改它,以便只在TextBox失去焦点时更新Name属性,而不是每次属性更改?

dev*_*tal 22

只需为该TextBoxCaliburn 实例明确设置绑定.Micro 不会触及它:

<TextBox Text="{Binding Name, UpdateSourceTrigger=LostFocus}" />
Run Code Online (Sandbox Code Playgroud)

或者,如果要更改所有实例的默认行为TextBox,则可以ConventionManager.ApplyUpdateSourceTrigger在引导程序的Configure方法中更改实现.

就像是:

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
  };
}
Run Code Online (Sandbox Code Playgroud)