Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 578387
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T14:15:17+00:00 2026-05-13T14:15:17+00:00

I am currently learning WPF (C#) and have taken the following simple data binding

  • 0

I am currently learning WPF (C#) and have taken the following simple data binding example from a text book which I am trying to get working.

The example displays a simple window containing a name and age textbox and a Birthday button.

The name and age that are display are represented in a Person object. When the birthday button is clicked the age is incremented and a message box is displayed.

The message box displays “Happy Birthday, [Name], age [++Age]”.

E.g. If the name on screen is Dangerous and the age is 28 then when you click the Birthday button the message box will state “Happy Birthday, Dangerous, age 29”.

The age is incremented correctly in the Person object but this information is not reflected back to the screen via the binding. However, if you alter the name or age field on the screen then this information is correctly updated in the Person object.

The example in the book states that I should see the updated age reflected back on the screen. I’m probably missing something simple but I have spent some time trying to correct this but don’t know what is wrong? Thanks in advance for any help with this example.

The XAML to display the window:

<Window x:Class="WpfApplication1.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1" Height="300" Width="300">
    <StackPanel Name="stack">
        <TextBlock>Name:</TextBlock>
        <TextBox Text="{Binding Path=Name}"/>
        <TextBlock>Age:</TextBlock>
        <TextBox Text="{Binding Path=Age}"/>
        <Button Name="birthdayButton">Birthday</Button>
    </StackPanel>
</Window>

The code behind:

using System; using System.Windows; using System.Windows.Controls;

namespace WpfApplication1 {
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class Window1 : Window
    {
        Person person = new Person("Dangerous", 28);

        public Window1()
        {
            InitializeComponent();

            stack.DataContext = person;

            this.birthdayButton.Click += birthdayButton_Click;
        }

        void birthdayButton_Click(object sender, RoutedEventArgs e)
        {
            ++person.Age;
            MessageBox.Show(
                string.Format("Happy Birthday, {0}, age {1}!",
                    person.Name,
                    person.Age),
                "Birthday");
        }
    }

    public class Person
    {
        string name;
        int age;

        public Person() { }

        public Person(string name, int age)
        {
            this.name = name;
            this.age = age;
        }

        public string Name
        {
            get {return this.name;}
            set {this.name = value;}
        }

        public int Age
        {
            get {return this.age;}
            set {this.age = value;}
        }
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T14:15:18+00:00Added an answer on May 13, 2026 at 2:15 pm

    First of all you need <TextBox Text="{Binding Path=Age, Mode=TwoWay}"/> Your Person object may also have to implement INotifyPropertyChanged and raise the proper event when the age changes. Check here for an example of how to implement INotifyPropertyChanged.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm currently learning algorithms from a book I have bought from Amazon but it
I am currently learning Haskell. I am trying to write a function which given
I am struggling with the learning curve on WPF data binding and could use
I'm currently learning WPF and have stumbled upon the concept of weak events but
im currently learning jQuery and have ran into a problem. I am trying to
Learning C#, WPF. Currently trying to get to grips with Dispatchers, specifically between threads
Im currently learning c++ from a book called 'Ivor Hortons Beginning Visual c++ 2010'.
im currently learning python (in the very begining), so I still have some doubts
im currently learning stacks in java and have a quick question. what will the
I am currently learning C, and am having some issues with trying to make

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.