当失去对文本框的关注时,ReactiveUI RaiseAndSetIfChanged命中

时间:2014-07-07 08:49:16

标签: mvvm reactiveui

我正在使用ReactiveCommand:

LoginCommand = new ReactiveCommand(this.WhenAny(x => x.UserName, x => x.Password, (r, g) => !string.IsNullOrEmpty(r.Value)  &&!string.IsNullOrEmpty(g.Value)));            
LoginCommand.Subscribe(async _ =>
{
var user = await _authenticationService.AuthenticateUser(_userName, _password);
                if (user.Error != null)
                {
                    MessageBox.Show("The user was not found. Please try again!", "User Not Found", MessageBoxButton.OK, MessageBoxImage.Exclamation);
                    return;
                }               
                screen.Router.Navigate.Execute(new MainViewModel(screen));
            });

用户名,用于绑定的密码字段:

public string Password
{
   get { return _password; }
   set { this.RaiseAndSetIfChanged(ref _password, value); }
}   

这个绑定的Textbox:

 <TextBox x:Name="txtPassword" Text="{Binding Password}" Grid.Column="1" Grid.Row="2" Margin="3,3,0,3" MinWidth="100" HorizontalAlignment="Left" Width="10" Height="30"/>

此代码有效,但当我失去对文本框的关注时。当我开始使用reactiveUI在文本框上书写时,是否有任何正确的方法。

1 个答案:

答案 0 :(得分:3)

使用UpdateSourceTrigger属性。顾名思义,这意味着只要TextBox的Text属性发生变化,就会触发RaiseAndSetIfChanged。

<TextBox x:Name="txtPassword" Text="{Binding Password, UpdateSourceTrigger=PropertyChanged}" Grid.Column="1" Grid.Row="2" Margin="3,3,0,3" MinWidth="100" HorizontalAlignment="Left" Width="10" Height="30"/>