Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF & MVVM: Get values from textboxes and send it to ViewModel

I'm trying to get the value of two Texboxes (I'm simulating a login window) when I press a button. The command assigned in the button fires correctly, but I don't know how to get the value of the textboxes to do the "login".

This is my ViewModel:

class LoginViewModel : BaseViewModel
{   
    public LoginViewModel()
    {

    }

    private DelegateCommand loginCommand;
    public ICommand LoginCommand
    {
        get
        {
            if (loginCommand == null)
                loginCommand = new DelegateCommand(new Action(LoginExecuted),
                               new Func<bool>(LoginCanExecute));
                return loginCommand;
            }
        } 

    public bool LoginCanExecute()
    {
        //Basic strings validation...
        return true;
    }
    public void LoginExecuted()
    {
        //Do the validation with the Database.
        System.Windows.MessageBox.Show("OK");
    } 
}

This is the view:

 <Grid DataContext="{StaticResource LoginViewModel}">

            <TextBox x:Name="LoginTxtBox" HorizontalAlignment="Left" Height="23" Margin="34,62,0,0" Width="154" />
            <PasswordBox x:Name="PasswordTxtBox" HorizontalAlignment="Left" Height="23" Margin="34,104,0,0" Width="154"/>
            <Button x:Name="btnAccept"
            HorizontalAlignment="Left" 
            Margin="34,153,0,0" 
            Width="108" 
            Content="{DynamicResource acceptBtn}" Height="31" BorderThickness="3"
            Command="{Binding LoginCommand}"/>

If somebody can help...I'll be infinitely grateful.

like image 275
Oscar Mateu Avatar asked Jul 01 '13 16:07

Oscar Mateu


People also ask

What is WPF used for?

What is WPF? WPF is a very rich UI framework which is used by developers to build Windows desktop applications. It comes with built-in support for graphics, resources, data binding and much other. It makes use of Extensible Markup Language to define views and it does it in a declarative way.

Is WPF .NET or .NET core?

WPF is a . NET Core UI framework for building Windows desktop applications.

Is WPF only C#?

WPF is used to build Windows client applications that run on Windows operating system. WPF uses XAML as its frontend language and C# as its backend languages.

What is replacing WPF?

Universal Windows Platform. Both Windows Forms and WPF are old, and Microsoft is pointing developers towards its Universal Windows Platform (UWP) instead. UWP is an evolution of the new application platform introduced in Windows 8 in 2012.


Video Answer


1 Answers

Typically, you'd bind the TextBox.Text properties to properties on your ViewModel. This way, the values are stored within the ViewModel, not the View, and there is no "getting" of the values required.

class LoginViewModel : BaseViewModel
{ 
    //...
    private string userName;
    public string UserName
    {
        get { return this.userName; }
        set 
        {
           // Implement with property changed handling for INotifyPropertyChanged
           if (!string.Equals(this.userName, value))
           {
               this.userName = value;
               this.RaisePropertyChanged(); // Method to raise the PropertyChanged event in your BaseViewModel class...
           }
        } 
    }

    // Same for Password...

Then, in your XAML, you'd do something like:

<TextBox Text="{Binding UserName}" HorizontalAlignment="Left" Height="23" Margin="34,62,0,0" Width="154" />
<PasswordBox Text="{Binding Password}" HorizontalAlignment="Left" Height="23" Margin="34,104,0,0" Width="154"/>

At this point, the LoginCommand can use the local properties directly.

like image 63
Reed Copsey Avatar answered Oct 18 '22 07:10

Reed Copsey