C# WPF绑定到类中的类成员

C# WPF绑定到类中的类成员,c#,wpf,mvvm,C#,Wpf,Mvvm,我第一次尝试C#、WPF和MVVM。我已经看过了一些答案和教程,但似乎无法正确理解 我有一个视图模型和一个视图模型文件(实际上更多,但试图简化)。在视图中,我想将文本框绑定到视图模型成员。我还想将按钮单击绑定到视图模型方法 用于登录()的委托命令工作正常,但我似乎无法更新Acc.ID中的ID属性。 我需要改变什么才能同时做到这两个 我知道我可能需要在ViewModel而不是模型中实现PropertyChanged事件……我只是不知道如何实现 我可以做的是在代码隐藏中将DataContext设置为

我第一次尝试C#、WPF和MVVM。我已经看过了一些答案和教程,但似乎无法正确理解

我有一个视图模型和一个视图模型文件(实际上更多,但试图简化)。在视图中,我想将文本框绑定到视图模型成员。我还想将按钮单击绑定到视图模型方法

用于
登录(
)的委托命令工作正常,但我似乎无法更新
Acc.ID
中的ID属性。 我需要改变什么才能同时做到这两个

我知道我可能需要在ViewModel而不是模型中实现
PropertyChanged
事件……我只是不知道如何实现

我可以做的是在代码隐藏中将
DataContext
设置为
user.Acc
,直接更新模型,但显然我无法绑定到
Login()
方法

ViewModel.cs

public class LoginVM
{
    private ServerInterface _serverInterface;

    private ICommand _loginCommand;

    private EmployeeAccount _acc;

    public ICommand LoginCommand
    {
        get { return _loginCommand; }
    }

    public LoginVM()
    {
        Acc = new EmployeeAccount();

        _serverInterface = new ServerInterface();

        _loginCommand = new DelegateCommand<String>(Login);
    }

    public EmployeeAccount Acc { get; set; }

    private void Login(object state)
    {

        this.Acc.ID = _serverInterface.Encrypt(this.Acc.ID);

    }
}
Model.cs

public class EmployeeAccount : INotifyPropertyChanged
{
    String _id;


    public EmployeeAccount()
    {
        ID = "5000";
        Name = "George Washington";
        isAdmin = true;
        Pswd = "TheyDont";
    }

    public String ID
    {
        get { return _id; }
        set
        {
            _id = value;
        this.OnPropertyChanged("ID");
        }
    }

    public string Name { get; set; }

    public Boolean isAdmin { get; set; }

    public string Pswd { get; set; }

    public event PropertyChangedEventHandler PropertyChanged;


    void OnPropertyChanged(string propName)
    {
        if (this.PropertyChanged != null)
            this.PropertyChanged(
                this, new PropertyChangedEventArgs(propName));
    }

}
.xaml只输入真正重要的内容

<TextBox x:Name="txtLogInName" Margin="60,43,42,129" TextWrapping="Wrap" Text="{Binding Path=ID, UpdateSourceTrigger=PropertyChanged}" Width="120" Grid.Column="1" Grid.Row="1"/>
        <Button x:Name="btnLogIn" Content="Log on" Command="{Binding LoginCommand}" Margin="160,151,10,23" Grid.Column="1" Grid.Row="1" RenderTransformOrigin="1.667,0.545"/>
        <TextBlock x:Name="txtBlockPassReset" TextAlignment="Center"  Grid.Row="1" Grid.Column="1" RenderTransformOrigin="1.348,1.765" Margin="60,101,42,78">
          <Hyperlink>Reset Password</Hyperlink>
        </TextBlock>
        <PasswordBox x:Name="pswdBoxLoginPass" Grid.Column="1" HorizontalAlignment="Left" Margin="60,72,0,0" Grid.Row="1" VerticalAlignment="Top" Width="120" Password="Password"/>

重置密码

尝试将xaml从

<TextBox x:Name="txtLogInName" Margin="60,43,42,129" TextWrapping="Wrap" 
    Text="{Binding Path=ID, UpdateSourceTrigger=PropertyChanged}" 
    Width="120" Grid.Column="1" Grid.Row="1"/>



您的代码中没有问题。登录方法应更新ID值。请发布您的XAML绑定代码以进一步调查。与ID的绑定是{binding Acc.ID}?或者显示您的xamlI为您发布的信息。Acc.ID根本没有得到更新……我想这是因为PropertyChanged事件的深度太深了一级。如果我将DataContext绑定到user.Acc,那么属性会更新,但无法在LoginVM.csTry中将方法绑定到
Ya…真是糟糕透了。我不知道在xaml中这是正确的语法用法。我还认为OnPropertyChanged()参数必须与绑定路径相同。不管怎么说,谢谢……请给出答案,我会接受的。
<TextBox x:Name="txtLogInName" Margin="60,43,42,129" TextWrapping="Wrap" 
    Text="{Binding Path=ID, UpdateSourceTrigger=PropertyChanged}" 
    Width="120" Grid.Column="1" Grid.Row="1"/>
<TextBox x:Name="txtLogInName" Margin="60,43,42,129" TextWrapping="Wrap" 
    Text="{Binding Path=Acc.ID, UpdateSourceTrigger=PropertyChanged}" 
    Width="120" Grid.Column="1" Grid.Row="1" />