C# WPF MVVM登录凭据

C# WPF MVVM登录凭据,c#,wpf,mvvm,C#,Wpf,Mvvm,我是WPF和MVVM的新手,我在理解方面正在取得进展。有一个问题让我有点困惑。我有一个带有关联ViewModel的登录页面,可以登录到应用程序。但是,每次我使用我知道是正确的凭据时,它都会返回“Username Incorrect”消息框。它应该打开到窗口主窗口。也许是文本框绑定的问题?任何帮助都将不胜感激。谢谢大家! 可观察对象 public abstract class ObservableObject : INotifyPropertyChanged { public event

我是WPF和MVVM的新手,我在理解方面正在取得进展。有一个问题让我有点困惑。我有一个带有关联ViewModel的登录页面,可以登录到应用程序。但是,每次我使用我知道是正确的凭据时,它都会返回“Username Incorrect”消息框。它应该打开到窗口主窗口。也许是文本框绑定的问题?任何帮助都将不胜感激。谢谢大家!

可观察对象

public abstract class ObservableObject : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    protected virtual void OnPropertyChanged(string propName)
    {
        Debug.Assert(GetType().GetProperty(propName) != null);

        var pc = PropertyChanged;
        if (pc != null)
        {
            pc(this, new PropertyChangedEventArgs(propName));
        }
    }

    protected bool SetProperty<T>(ref T field, T value, string propName)
    {
        if (!EqualityComparer<T>.Default.Equals(field, value))
        {
            field = value;
            OnPropertyChanged(propName);
            return true;
        }

        return false;
    }

    protected bool SetProperty<T>(ref T field, T value, Expression<Func<T>> expr)
    {
        if (!EqualityComparer<T>.Default.Equals(field, value))
        {
            field = value;
            var lambda = (LambdaExpression)expr;
            MemberExpression memberExpr;

            if (lambda.Body is UnaryExpression)
            {
                var unaryExpr = (UnaryExpression)lambda.Body;
                memberExpr = (MemberExpression)unaryExpr.Operand;
            }
            else
            {
                memberExpr = (MemberExpression)lambda.Body;
            }

            OnPropertyChanged(memberExpr.Member.Name);
            return true;
        }

        return false;
    }
}
查看

    <Grid>
    <Label x:Name="lblUsername" Content="Username" HorizontalAlignment="Left" Margin="45,57,0,0" VerticalAlignment="Top"/>
    <Label x:Name="lblPassword" Content="Password" HorizontalAlignment="Left" Margin="48,104,0,0" VerticalAlignment="Top"/>
    <TextBox x:Name="txtUsername" Text="{Binding Textbox1Input, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" 
             HorizontalAlignment="Left" Height="23" Margin="128,60,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120"/>
    <TextBox x:Name="txtPassword" Text="{Binding Textbox2Input, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" 
             HorizontalAlignment="Left" Height="23" Margin="128,108,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120"/>
    <Button x:Name="btnSubmit" Content="Submit" HorizontalAlignment="Left" Margin="118,175,0,0" VerticalAlignment="Top" Width="75"
            Command="{Binding SubmitLoginButton}"
            />
</Grid>

您正在绑定到
TextBox
Text属性,该属性期望
string
作为源,并且您正在绑定到
TextBox
本身。 只需将其更改为字符串属性:

private String_textbox1Input;

public String Textbox1Input
{
    get { return _textbox1Input; }
    set { SetProperty(ref _textbox1Input, value, () => Textbox1Input); }
}

注意:文本框默认绑定模式为双向显式设置没有用

就是这样!非常感谢你!
    <Grid>
    <Label x:Name="lblUsername" Content="Username" HorizontalAlignment="Left" Margin="45,57,0,0" VerticalAlignment="Top"/>
    <Label x:Name="lblPassword" Content="Password" HorizontalAlignment="Left" Margin="48,104,0,0" VerticalAlignment="Top"/>
    <TextBox x:Name="txtUsername" Text="{Binding Textbox1Input, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" 
             HorizontalAlignment="Left" Height="23" Margin="128,60,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120"/>
    <TextBox x:Name="txtPassword" Text="{Binding Textbox2Input, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" 
             HorizontalAlignment="Left" Height="23" Margin="128,108,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120"/>
    <Button x:Name="btnSubmit" Content="Submit" HorizontalAlignment="Left" Margin="118,175,0,0" VerticalAlignment="Top" Width="75"
            Command="{Binding SubmitLoginButton}"
            />
</Grid>
    public partial class LoginWindow : Window
{
    private LoginWindowVM _viewModel;

    public LoginWindow()
    {
        InitializeComponent();

        _viewModel = new LoginWindowVM();
        this.DataContext = _viewModel;
    }
}
private String_textbox1Input;

public String Textbox1Input
{
    get { return _textbox1Input; }
    set { SetProperty(ref _textbox1Input, value, () => Textbox1Input); }
}