mvvm - ReactiveUI RaiseAndSetIfChanged 在文本框失去焦点时命中

标签 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));
            });

以及用于绑定(bind)的用户名、密码字段:
public string Password
{
   get { return _password; }
   set { this.RaiseAndSetIfChanged(ref _password, value); }
}   

并将此绑定(bind)的文本框用于:
 <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"/>

此代码有效,但是当我失去对文本框的关注时。当我开始使用响应式 UI 在文本框上写作时,有什么合适的方法可以做到这一点。

最佳答案

使用 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"/>

关于mvvm - ReactiveUI RaiseAndSetIfChanged 在文本框失去焦点时命中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24606512/

相关文章:

c# - 在基类中拥有所有依赖项是一种好习惯吗?

.net - 响应式(Reactive)UI 9 : binding lists to a WPF view

dynamic-data - Observable ChangeSet 等待列表准备好后再观看

c# - 通过 MVVM 模式缩放绘图?

wpf - ViewModel 到 ViewModel 的通信

绑定(bind)源的 WPF Treeview (MVVM) 实时表示(分组)

c# - 在 WPF MVVM 中使用 ReactiveUI 获取属性更改的先验值

c# - ReactiveUI:从自定义对话框返回值

ios - 实现不同的单元类型

c# - UWP 应用程序中的高级 MVVM 场景 - 如何将模型类用于多个 View 模型?