2010-12-22 27 views
1

當我輸入使用DefaultButton(編輯登錄+ 標籤,編輯密碼+ 輸入)登錄密碼,該X.Password屬性仍沒有改變。那麼我在使用DefaultButton時如何提交密碼?MVVM默認按鈕比文本框更快提交文本視圖模型

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

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

繼電器命令

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) 

回答

6

被設定(缺省),以僅更新當文本框失去焦點的結合。當您按Enter鍵時,TextBox不會失去焦點。你可以告訴文本框解決這一問題儘快更新綁定值,因爲它改變了,就像這樣:

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

而且我也不需要「設定值= 密碼< - 價值 X.OnPropertyChanged‘密碼’ 「 然後 ?所以我需要一些時間來嘗試,謝謝 – Cynede 2011-02-24 05:22:45