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>
在公共场合public MainWindow()
添加了this.DataContext = this
及其工作
好像
public MainWindow()
{
InitializeComponent();
_name = "Adam";
PersonName = "Roman";
this.DataContext = this;
}