不将结果从View传递到ViewModel MVVM light

时间:2019-05-31 08:28:02

标签: c# wpf 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>

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

1 个答案:

答案 0 :(得分:1)

绑定路径区分大小写。您应该绑定到User.First n ame或将属性名称更改为First N ame:

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