это мой простой xaml, который показывает в текстовом поле возраст первого человека в группе людей. Я не понимаю, что после клика возраст не меняется.
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="132*" />
<RowDefinition Height="179*" />
</Grid.RowDefinitions>
<TextBlock Text="{Binding Persons[0].Age}" />
<Button Grid.Row="1" Click="Button_Click">Change Age</Button>
</Grid>
это код позади xaml:
public partial class MainWindow : Window
{
public ObservableCollection<Person> Persons { get; set; }
public MainWindow() {
Persons = new ObservableCollection<Person>();
Persons.Add(new Person{Age = -1});
DataContext = this;
InitializeComponent();
}
private void Button_Click(object sender, RoutedEventArgs e) {
(Persons[0] as Person).Age = 5;
}
}
это классный человек:
public class Person : INotifyPropertyChanged
{
private int _age;
public int Age
{
get { return _age; }
set
{
_age = value;
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs("Age"));
}
}
}
#region INotifyPropertyChanged Members
public event PropertyChangedEventHandler PropertyChanged;
#endregion
}