【问题标题】:Caliburn Micro: how to set binding UpdateSourceTrigger?Caliburn Micro:如何设置绑定 UpdateSourceTrigger?
【发布时间】:2011-06-29 07:57:59
【问题描述】:

我一直在探索 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 属性,而不是每次属性更改时更新?

【问题讨论】:

    标签: wpf mvvm caliburn.micro


    【解决方案1】:

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

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

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

    类似:

    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
      };
    }
    

    【讨论】:

    • 好的,谢谢,我知道我总是可以回退到使用普通方法,我只是想知道是否有“Caliburn 方法”。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-12
    • 2018-07-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多