为什么 PropertyChanged 始终为空?
Why PropertyChanged is always null?
PropertyChanged 始终为 null,名称未显示在 window 上。应该是罗马字。知道为什么总是null不显示名字吗?
我的windowsclass
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 public MainWindow()
中添加了 this.DataContext = this
及其工作方式
看起来像
public MainWindow()
{
InitializeComponent();
_name = "Adam";
PersonName = "Roman";
this.DataContext = this;
}
PropertyChanged 始终为 null,名称未显示在 window 上。应该是罗马字。知道为什么总是null不显示名字吗?
我的windowsclass
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 public MainWindow()
中添加了 this.DataContext = this
及其工作方式
看起来像
public MainWindow()
{
InitializeComponent();
_name = "Adam";
PersonName = "Roman";
this.DataContext = this;
}