WPF绑定到类属性

时间:2016-02-18 17:56:05

标签: c# wpf mvvm data-binding inotifypropertychanged

我有xaml文件:

<TextBox Text="{Binding Student.SName, Mode=TwoWay}"/>
<TextBox Text="{Binding Student.Name, Mode=TwoWay}"/>

我对这个xaml文件的定义:

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");
            }
        }
    }
}

如何在输入文字时自动更改学生资产中的值?

0 个答案:

没有答案