为什么PropertyChanged总是为空?

问题描述 投票:1回答:1

PropertyChanged始终为null,并且窗口中不显示名称。它应该是罗马人。你知道为什么它总是空的而不是扯掉这个名字吗?

我的windows类

 public partial class MainWindow : Window, INotifyPropertyChanged
{

    string _name;
    public event PropertyChangedEventHandler PropertyChanged;

    public string PersonName
    {
        get => _name;
        set
        {
            _name = value;
            OnPropertyChanged("PersonName");
        }
    }

    public MainWindow()
    {

        InitializeComponent();
        _name = "Adam";
        PersonName = "Roman";
    }

    protected void OnPropertyChanged(string name)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null)
        {
            handler(this, new PropertyChangedEventArgs(name));
        }
    }
}

我的XAML

<Grid>
    <Label x:Name="Test" Width="100" Height="50" Foreground="Black" Content="{Binding PersonName, Mode=TwoWay}"></Label>
</Grid>
c# wpf inotifypropertychanged
1个回答
0
投票

在公共场合public MainWindow()添加了this.DataContext = this及其工作

好像

 public MainWindow()
    {
        InitializeComponent();
        _name = "Adam";
        PersonName = "Roman";
        this.DataContext = this;
    }
© www.soinside.com 2019 - 2024. All rights reserved.