开发者

MVVM Default Button faster than TextBox commit text to ViewModel

开发者 https://www.devze.com 2023-01-31 22:59 出处:网络
When I enter login password using DefaultButton (edit login + Tab, edit password + Enter), the X.Password property is still not changed. So how can I commit password when I\'m using DefaultButton?

When I enter login password using DefaultButton (edit login + Tab, edit password + Enter), the X.Password property is still not changed. So how can I commit password when I'm using 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
            开发者_StackOverflow社区            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" />

VMBase

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) |])

Relay command

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)


The binding is set (by default) to only update when the TextBox loses focus. The TextBox does not lose focus when you hit the enter key. You can fix this by telling the textboxes to update the bound value as soon as it changes, like so:

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

精彩评论

暂无评论...
验证码 换一张
取 消