9

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

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

我的代码很简单,例如这里是我的虚拟机:

public class ShellViewModel : PropertyChangeBase
{
    private string _name;

    public string Name
    {
        get { return _name; }
        set 
        { 
            _name = value; 
            NotifyOfPropertyChange(() => Name);
        }
    }
}

在我看来,我有一个简单的文本框。

<TextBox x:Name="Name" />

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

4

1 回答 1

23

只需为该实例显式设置绑定TextBox,Caliburn.Micro 就不会触及它:

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

或者,如果您想更改 的所有实例的默认行为,那么您可以在引导程序的方法TextBox中更改 的实现。ConventionManager.ApplyUpdateSourceTriggerConfigure

就像是:

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
  };
}
于 2011-02-18T12:14:01.827 回答