[Solved] Login-View with .IsDefault Button

Nov 9, 2015 at 2:12 PM
Hello,

i use a small viel to generate a popup to input the credentials:
<mvvm:View xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
        
        xmlns:mvvm="clr-namespace:CODE.Framework.Wpf.Mvvm;assembly=CODE.Framework.Wpf.Mvvm"
        xmlns:controls="clr-namespace:CODE.Framework.Wpf.Controls;assembly=CODE.Framework.Wpf"
...
        Title="{lex:Loc LoginLogin_Title}" 
        IconResourceKey="Icon-Lock"
        Style="{DynamicResource CODE.Framework-Layout-SimpleFormLayout}">

        
    <Label Content="{lex:Loc LoginLogin_Username}"/>
    <TextBox ToolTip="{lex:Loc LoginLogin_UsernameTip}" Text="{Binding Username}" Width="250" />

    <Label Content="{lex:Loc LoginLogin_Password}"/>
    <PasswordBox ToolTip="{lex:Loc LoginLogin_PasswordTip}" controls:PasswordBoxEx.Value="{Binding Password}" Width="250" />
    <!-- In dem ViewModel darf der Initialwert der Passwordbox-Property nicht gesetzt werden, sonst
         kommt die Bindung nicht korrekt zustande -->


</mvvm:View>
Now I want to open it with the Default-Button "Login":
Public Class LoginViewModel
    Inherits ViewModel


    Public Sub New()

        ' Setzen der Aktionen
        Actions.Add(New ViewAction("Optionen", execute:=Sub(action, parameter) Controller.Action("Options", "AllOverSettings")))

        Actions.Add(New ViewAction("Login", execute:=Sub(action, parameter) Login(action, parameter)) With { _
             .Significance = ViewActionSignificance.Normal,
             ___.IsDefault = True___
        })
        ' Bei der Verwendung der Passwordbox als letztes Eingabefeld, ist die Verwendung von '.IsDefault' nicht möglich, da sonst der 
        ' Wert nicht in die Eigenschaft geschrieben wird

        Actions.Add(New ApplicationShutdownViewAction("Abbrechen"))

    End Sub
If I set the IsDeafult-Property then then the input of the password will notwritten to the Password-Property. If I remove the IsDefault and the User is pressing "Login"-Button with the Mouse, then it works fine.

How can I set the Default-Button correcly if i use the Passwordbox
Nov 9, 2015 at 4:46 PM
If you want to focus on password box; in controller before the ActionResult returns with the view and view model you can use the following command for focusing your password box; but dont forget to name your password box and your button still can have IsDefault property.
 public ActionResult Login()
        {
            var model = new LoginViewModel();
            var view = ViewModal(model, ViewLevel.Popup);
            var passwordBox = view.View.FindName("uxPBPassword");
            if (passwordBox != null && passwordBox is PasswordBox)
            {
                    ((PasswordBox)passwordBox).Focus();
            }
            view.ViewTitle = model.Title;
            return view;
        }
Nov 9, 2015 at 5:01 PM
Thanks for your answer. But I dont wont to set the focus to the password-box. I want to set the default-command.

If the user press "enter" then the action on the login-button should be exeuted.
Nov 11, 2015 at 5:02 PM
I think that you've done everything correctly but the problem is that hitting the <return> key doesn't cause the textbox to lose focus and, as a result, the data in the model is not being updated ... the default binding behavior for textboxes. One solution is to set the UpdateSourceTrigger in the XAML textbox binding to PropertyChanged:
    <Label Content="{lex:Loc LoginLogin_Password}"/>
    <PasswordBox ToolTip="{lex:Loc LoginLogin_PasswordTip}"
                controls:PasswordBoxEx.Value="{Binding Password, UpdateSourceTrigger=PropertyChanged}}" Width="250" />
This will update the data in the model at every keystroke so the data is available when the <return> key is pressed. It may be a good idea to make a similar change to the Username binding in case the user makes an edit there then presses <return>.
Marked as answer by MyKey0815 on 11/11/2015 at 1:17 PM
Nov 11, 2015 at 9:19 PM
Thanks, Spakamundo. Your Tip was the solution of the problem. Now it works with the ".IsDefault"-Property