WPF - Binding problem
- by Erez
Why after clicking the button the text in the textblock doesn't change ?
XAML:
<Grid>
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<TextBlock Text="{Binding Name}"/>
<Button Click="Button_Click" Grid.Row="1" Margin="20">Click Me</Button>
</Grid>
Code behind:
public partial class Window1 : Window, INotifyPropertyChanged
{
private Person _myPerson;
public Person MyPerson
{
get { return _myPerson; }
set
{
_myPerson = value;
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs("MyPerson"));
}
}
}
public Window1()
{
MyPerson = new Person { Name = "A" };
DataContext = MyPerson;
InitializeComponent();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
MyPerson = new Person { Name = "B" };
}
#region INotifyPropertyChanged Members
public event PropertyChangedEventHandler PropertyChanged;
#endregion
}