不将结果从 View 带到 ViewModel MVVM light

Doesn't take the result from View to ViewModel MVVM light

我的 ViewModel 没有从模型中获取值

我创建模型

public class UserModel : INotifyPropertyChanged
{
    private string firstName;


    public string Firstname
    {
        get => firstName;
        set
        {
            firstName = value;
            NotifyPropertyChanged("Firstname");
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;

    private void NotifyPropertyChanged(string propertyName = "")
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}

我的视图模型

UserModel user = new UserModel();
public UserModel User
{
    get => user;
    set => Set(ref user, value);
}

在模型中我绑定了这条线 User.FirstName

<TextBox x:Name="FirstName" Style="{StaticResource FirstNameBox}" Grid.Column="2" Grid.Row="2" >
    <TextBox.Text>
        <Binding  Mode="TwoWay" Path="User.FirstName" UpdateSourceTrigger="PropertyChanged">
            <Binding.ValidationRules>
                <DataErrorValidationRule ValidatesOnTargetUpdated="False"/>
            </Binding.ValidationRules>
        </Binding>
    </TextBox.Text>

但我取null。以这个答案为例 mvvm calculated fields

绑定路径是case-sensitive。您应该绑定到 User.Firstname 或将 属性 的名称更改为 FirstName:

public string FirstName
{
    get => firstName;
    set
    {
        firstName = value;
        NotifyPropertyChanged("Firstname");
    }
}