WPF & MVVM:从文本框中获取值并将其发送到 ViewModel

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17409010/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 23:51:40  来源:igfitidea点击:

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

wpfmvvmicommanddelegatecommand

提问by Oscar Mateu

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

当我按下按钮时,我试图获取两个 Texbox 的值(我正在模拟登录窗口)。按钮中分配的命令正确触发,但我不知道如何获取文本框的值来执行“登录”。

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.

如果有人可以提供帮助......我将不胜感激。

回答by Reed Copsey

Typically, you'd bind the TextBox.Textproperties 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.

通常,TextBox.Text您会将属性绑定到 ViewModel 上的属性。这样,值存储在 ViewModel 而不是 View 中,并且不需要“获取”所需的值。

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:

然后,在您的 XAML 中,您将执行以下操作:

<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 LoginCommandcan use the local properties directly.

此时,LoginCommand可以直接使用本地属性。