文本框数据绑定到另一个类的属性

时间:2018-07-08 13:43:25

标签: c# wpf binding

将另一个类的属性绑定到我的TextBox时遇到了一个小问题。

例如,这是代码(Person只是一个示例类):

public class wndMain : INotifyPropertyChanged
{
    private Person _Max;
    public Person Max
    {
        get
        {
            return _Max;
        }
        set
        {
            _Max = value;
            OnPropertyChanged("Max");
        }
    }

    public wndMain()
    {
        InitializeComponent();
        Initialize();
    }

    private void Initialize()
    {
        Max = new Person()
        {
            FirstName = "Max",
            LastName = "Mustermann",
            Age = 21
        };
    }

    public event PropertyChangedEventHandler PropertyChanged;
    private void OnPropertyChanged([CallerMemberName] string sPropertyName = null)
    {
        if(this.PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(sPropertyName));
        }
    }
}

现在我想将人员对象实例的名字绑定到我的TextBox上,绑定看起来像这样:

"{Binding Path=Max.FirstName, Mode=TwoWay}"

但是什么也没发生-我也将INotifyPropertyChanged包含在person类中,但这也无济于事。

您有什么建议吗?

0 个答案:

没有答案