wpf - MVVM 默认按钮比 TextBox 更快地将文本提交到 ViewModel

标签 wpf mvvm f#

当我使用 DefaultButton(编辑登录名 + Tab,编辑密码 + Enter)输入登录密码时,X.Password属性仍然没有改变。那么当我使用 DefaultButton 时如何提交密码呢?

member X.Password
    with get()      = password
    and set value   = 
        password <- value
        X.OnPropertyChanged "Password"

member X.LoginCommand =
    new RelayCommand ((fun canExecute -> true), (fun action ->
            X.SelectedAccount <-
                match
                    X.Accounts
                    |> Seq.filter (fun acc -> 
                        acc.Name        = login && 
                        acc.Password    = password) with
                    | s when Seq.isEmpty s -> 
                        X.ConvertButtonEnabled <- false
                        ignore <| MessageBox.Show(sprintf 
                            "User %s doesn't exist or password incorrect password" X.Login) 
                        {Name=""; Role=""; Password=""; ExpenseLineItems = []}
                    | s -> 
                        X.ConvertButtonEnabled <- true
                        X.LoginExpander <- false
                        Seq.head s

            X.Login     <- ""
            X.Password  <- "" ))

XAML:
            <Button Content="Login" Command="{Binding LoginCommand}" Height="23" HorizontalAlignment="Left" Margin="79,71,0,0" Name="LoginButton" VerticalAlignment="Top" Width="75" IsDefault="True" />
            <TextBox Text="{Binding Login}" Height="28" HorizontalAlignment="Left" Margin="61,6,0,0" Name="Login" VerticalAlignment="Top" Width="142" />
            <TextBox Text="{Binding Password}" Height="26" HorizontalAlignment="Left" Margin="61,34,0,0" Name="Password" VerticalAlignment="Top" Width="142" />

虚拟机库
type ViewModelBase() =
    let propertyChangedEvent = new DelegateEvent<PropertyChangedEventHandler>()
    interface INotifyPropertyChanged with
        [<CLIEvent>]
        member x.PropertyChanged = propertyChangedEvent.Publish

    member x.OnPropertyChanged propertyName = 
        propertyChangedEvent.Trigger([| x; new PropertyChangedEventArgs(propertyName) |])

继电器指令
type RelayCommand (canExecute:(obj -> bool), action:(obj -> unit)) =
    let event = new DelegateEvent<EventHandler>()
    interface ICommand with
        [<CLIEvent>]
        member x.CanExecuteChanged = event.Publish
        member x.CanExecute arg = canExecute(arg)
        member x.Execute arg = action(arg)

最佳答案

绑定(bind)设置(默认情况下)仅在 TextBox 失去焦点时更新。当您按下回车键时,文本框不会失去焦点。您可以通过告诉文本框在绑定(bind)值更改时立即更新它来解决此问题,如下所示:

   <TextBox Text="{Binding Login, UpdateSourceTrigger=PropertyChanged}"/>
   <TextBox Text="{Binding Password, UpdateSourceTrigger=PropertyChanged}"/>

关于wpf - MVVM 默认按钮比 TextBox 更快地将文本提交到 ViewModel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4507227/

相关文章:

silverlight-4.0 - 非通用 CompositePresentationEvent 和 EventSubscription?

F#使用GetEnumerator时无法枚举yield生成的序列

c# - 我无法使用 INotifyPropertyChanged 更新我的值

c# - 在自己的控制中传递验证错误

c# - 使用 ninject 作为 WPF View 模式定位器 - View 模型作为单例

c# - 当我的设置也更改时,如何更改MVVM属性?

F# 程序在 fsi 中正确运行,但作为 exe 挂起

f# - 为什么我的 Actor 没有在 akka.net 中使用 F# 异常停止

c# - DataGridComboBoxColumn 绑定(bind)到列表<String>

WPF 错误模板未显示