我正在学习WPF,我编写了一个使用"Bindings"概念的小例子.但该程序的行为与我的预期不同.
我有Person类,它有两个属性:FirstName和LastName.这个类实现了INotifyPropertyChanged接口,因此我可以用它来绑定.
class Person : INotifyPropertyChanged
{
private string firstName;
private string lastName;
public string FirstName
{
get { return firstName; }
set { NotifyPropertyChanged("FirstName"); firstName = value; }
}
public string LastName
{
get { return lastName; }
set { NotifyPropertyChanged("LastName"); lastName = value; }
}
private void NotifyPropertyChanged(string info)
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(info));
}
public event PropertyChangedEventHandler PropertyChanged;
}
Run Code Online (Sandbox Code Playgroud)
然后我有一些XAML代码.
<Window x:Class="BughouseClient.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<StackPanel Name="stackPanel">
<TextBox Text="{Binding Path=FirstName, UpdateSourceTrigger=PropertyChanged}"> …Run Code Online (Sandbox Code Playgroud)