Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/309.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# WPF绑定到类属性_C#_Wpf_Mvvm_Data Binding_Inotifypropertychanged - Fatal编程技术网

C# WPF绑定到类属性

C# WPF绑定到类属性,c#,wpf,mvvm,data-binding,inotifypropertychanged,C#,Wpf,Mvvm,Data Binding,Inotifypropertychanged,我有一个xaml文件: <TextBox Text="{Binding Student.SName, Mode=TwoWay}"/> <TextBox Text="{Binding Student.Name, Mode=TwoWay}"/> public Model.Student Student { get; set; } 以及属性中的Student类: public class Student : INotifyPropertyChanged { publ

我有一个xaml文件:

<TextBox Text="{Binding Student.SName, Mode=TwoWay}"/>
<TextBox Text="{Binding Student.Name, Mode=TwoWay}"/>
public Model.Student Student { get; set; }
以及属性中的
Student
类:

public class Student : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;
    protected virtual void OnPropertyChanged(string propertyName)
    {
       if (PropertyChanged != null)
       {
           PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
       }
    }

    private string _SName;
    private string _Name;

    public string SName
    {
        get { return _SName; }
        set
        {
            if (_SName != value)
            {
                _SName = value;
                OnPropertyChanged("SName");
            }
        }
    }

    public string Name
    {
        get { return _Name; }
        set
        {
            if (_Name != value)
            {
               _Name = value;
               OnPropertyChanged("Name");
            }
        }
    }
}

我能做什么,在输入文本时自动更改Student属性中的值?

@EhsanSajjad当我在.cs文件视图更新中添加信息时,但当我在TextBox中写入文本时,它不会为我更新;(您可以将“UpdateSourceTrigger”设置为“Lost focus”,将绑定的
UpdateSourceTrigger
属性设置为
PropertyChanged
。默认情况下,它是
LostFocus
,只有在文本框失去焦点时才会触发绑定源(即视图模型)属性的更新。
Text=”{Binding Student.Name,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}”
只是想澄清一下,您有一个
AddStudentViewModel
类,它包含
Student
的属性,这就是您所说的没有更新的属性?因为在您的问题中,您说
Student
属性的定义在XAML中,这使我认为您的DataContext设置不正确,这是一个错误d您可能正在查看
Student
对象的两个独立实例。