WPF 绑定到类中的类的成员

本文关键字:成员 绑定 WPF | 更新日期: 2023-09-27 18:31:57

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

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

Login( ) 的委托命令工作正常,但我似乎无法更新 Acc.ID 中的 ID 属性。我需要更改什么才能同时执行这两项操作?

我知道我可能需要在视图模型中实现PropertyChanged事件而不是模型......我只是不明白如何。

我能做的是在后面的代码中设置DataContext user.Acc以直接更新模型,但是我显然无法绑定到Login()方法。

视图模型.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);
    }
}

View.xaml.cs

 public partial class LoginView : Window
{
    public LoginView()
    {
        InitializeComponent();
        BindInXaml();
    }
    private void BindInXaml()
    {
        base.DataContext = new LoginVM();
    }
}

型号.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"/>

WPF 绑定到类中的类的成员

尝试将 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"/>

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