C# WPF绑定到类属性
我有一个xaml文件: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
<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
对象的两个独立实例。