WPF - Why are changes to an object for a simple data-binding not reflected back to the target in the main window? -


I am currently learning WPF (C #) and have taken following simple data binding examples from a text book that I have to do. Trying to work

The example shows a simple window containing a name and age text box and a birthday button.

The names and ages displayed are shown in an individual object. When birthday button is clicked, the age increases and a message box is displayed.

Displays the message box "Happy Birthday, [Name], Age [++ Age]".

For example, if the name on the screen is dangerous and age is 28, then when you click on the birthday button, the message box will say "Happy Birthday, Dangerous, Age 29"

Individual object has increased accurately but this information does not appear back on the screen through binding. However, if you change the name or age field on the screen, then this information has been updated correctly in the person's object.

The examples given in the book illustrate that I should see the updated age back on screen. I am probably missing with some simplicity, but I have spent some time trying to fix it but do not know what is wrong? Thanks in advance for any help with this example.

XAML to display the window:

  & lt; Window x: orbit = "wpfApplication1.Window1" xmlns = "http: //schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns: x = "http://schemas.microsoft.com/winfx/2006/ Xaml "title =" window 1 "height =" 300 "width =" 300 "& gt; & Lt; Stackpanel name = "stack" & gt; & Lt; TextBlock & gt; Name: & lt; / TextBlock & gt; & Lt; Text box text = "{binding path = name}" /> & Lt; TextBlock & gt; Age: & lt; / TextBlock & gt; & Lt; Text box text = "{binding path = age}" /> & Lt; Button name = "birthday button" & gt; Birthday & lt; / Button & gt; & Lt; / StackPanel & gt; & Lt; / Window & gt;  

Back code:

  Using the system; Using System.Windows; Using System.Windows.Controls; Namespace WpfApplication1 {/// & lt; Summary & gt; /// Window 1. Interaction logic for xaml /// & lt; / Summary & gt; Public partial squares window 1: window {person person = new person ("dangerous", 28); Public Window 1 () {InitializeComponent (); Stack.DataContext = Person; This.birthdayButton.Click + = birthdayButton_Click; } Zero Birthday Batalglick (Object Sender, Routing Avenger, ARG) {++ person.Age; MessageBox.Show (string.Format ("Happy birthday, {0}, age {1}!", Person .name, person.ege), "birthday"); }} Public class person {string name; Old age; Public person () {} public person (string name, int age) {this.name = name; This.age = age; } Public string name {get {this.name; Return}} {this.name = value;}} public int age {get {this.age; Return}} {this.age = value}}}}  & lt; TextBox Text = "{Binding Path = Age Required,   

The object person may need to implement the INotifyPropertyChanged and when the age changes, the appropriate event can be extended, for example, implementing the INotifyPropertyChanged for.


Comments

Popular posts from this blog

sql - dynamically varied number of conditions in the 'where' statement using LINQ -

asp.net mvc - Dynamically Generated Ajax.BeginForm -

Debug on symbian -