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 6030347
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T05:01:25+00:00 2026-05-23T05:01:25+00:00

I am playing with databinding in wpf and I encountered a problem. Here is

  • 0

I am playing with databinding in wpf and I encountered a problem. Here is my code:

MainWindow.xaml

<Window x:Class="TestWpf.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:TestWpf"
        Title="MainWindow" Height="350" Width="525">    
    <StackPanel Name="stackpanel">
        <TextBox Name="tb1" Text="{Binding Path=A.Number}" />
        <TextBox Name="tb2" Text="{Binding Path=B.Number}" />
        <TextBlock Name="tbResult" Text="{Binding Path=C}" />
    </StackPanel>
</Window>

MainWindow.xaml.cs

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        MyClass myClass = new MyClass();
        myClass.A = new MySubClass();
        myClass.B = new MySubClass();
        stackpanel.DataContext = myClass;
    }
}

MyClass.cs

class MyClass : INotifyPropertyChanged
{
    private MySubClass a;
    public MySubClass A 
    {
        get { return a; }
        set
        {
            a = value;
            OnPropertyChanged("A");
            OnPropertyChanged("C");
        }
    }

    private MySubClass b;
    public MySubClass B 
    {
        get { return b; }
        set
        {
            b = value;
            OnPropertyChanged("B");
            OnPropertyChanged("C");
        }
    }

    public int C
    {
        get { return A.Number + B.Number; }
    }

    public event PropertyChangedEventHandler PropertyChanged;
    private void OnPropertyChanged(string p)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(p));
        }
    }        
}

MySubClass.cs

class MySubClass : INotifyPropertyChanged
{
    private int number;
    public int Number 
    {
        get { return number; }
        set
        {
            number = value;
            OnPropertyChanged("Number");
        }
    }

    public MySubClass()
    {
        Number = 1;
    }

    public event PropertyChangedEventHandler PropertyChanged;
    private void OnPropertyChanged(string p)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(p));
        }
    }     
}

Now, the problem is that binding works fine, after I run the app. Also, values A.Number and B.Number are updating fine as I change them in textbox. But variable C in MyClass.C is updated only when app starts and never after. What do I need to change so that C updates when I change A.Number or B.Number. Thanks.

  • 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-23T05:01:26+00:00Added an answer on May 23, 2026 at 5:01 am

    When updating your data model, do you change the Number directly on the MySubClass instances or do you assign new subclasses to the MyClass instance? i.e:

    myClass.A.Number = 5; // will trigger OnPropertyChanged on MySubClass, but not on MyClass
    

    will not trigger OnPropertyChanged on the A (but will of course update A.Number).
    For this to work you have to do something like:

    MySubClass v = new MySubClass()
    v.Number = 5;
    myClass.A = v;  // will trigger OnPropertyChanged on MyClass
    

    Updated answer. YOu could do this to catch any property changes in a and b.

    public MyClass()
    {
        a.PropertyChanged += new PropertyChangedEventHandler(UpdateC);
        b.PropertyChanged += new PropertyChangedEventHandler(UpdateC);
    }
    
    void UpdateC(object sender, PropertyChangedEventArgs e)
    {
        OnPropertyChanged("C");
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm playing around with WPF and databinding and I'm wondering about the following. I
I am playing about with WPF and databinding a Label control's content: <Label Content={Binding
Im playing a little bit with heavy-client app. Imagine I have this model: class
Still playing with Chrome Extension and have a little problem when I want to
been playing with http://openidenabled.com/php-openid/trunk/examples/consumer and tried the following: flim.blogspot.com flimcc.blogspot.com The first works, the
Hey guys, I've been playing around with WPF's Path shape, but I'm a bit
Playing with Disqus for commenting, but the problem is that we have a Silverlight
When playing with WPF, I discovered the SizeToContent method, which enables me to create
I playing around with the sftp example from here: Stackoverflow: twisted conch filetransfer I
Recently playing around with the open source iphone app code, and found it uses

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.