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

  • Home
  • SEARCH
  • 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 6118159
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T15:24:13+00:00 2026-05-23T15:24:13+00:00

This is an example code: public class MyParent : INotifyPropertyChanged { List<MyChild> MyChildren; public

  • 0

This is an example code:

public class MyParent : INotifyPropertyChanged
{
    List<MyChild> MyChildren;

    public bool IsChanged
    {
        get
        {
            foreach (var child in MyChildren)
            {
                if (child.IsChanged) return true;
            }
            return false;
        }
    }


    public event PropertyChangedEventHandler PropertyChanged;
    private void RaiseChanged(string propName)
    {
        if (PropertyChanged != null)
            PropertyChanged.Invoke(this, new PropertyChangedEventArgs(propName));
    }
}

public class MyChild : INotifyPropertyChanged
{
    private int _Value;
    public int Value
    {
        get
        {
            return _Value;
        }
        set
        {
            if (_Value == value)
                return;
            _Value = value;
            RaiseChanged("Value");
            RaiseChanged("IsChanged");
        }
    }
    private int _DefaultValue;
    public int DefaultValue
    {
        get
        {
            return _DefaultValue;
        }
        set
        {
            if (_DefaultValue == value)
                return;
            _DefaultValue = value;
            RaiseChanged("DefaultValue");
            RaiseChanged("IsChanged");
        }
    }

    public bool IsChanged
    {
        get
        {
            return (Value != DefaultValue);
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;
    private void RaiseChanged(string propName)
    {
        if (PropertyChanged != null)
            PropertyChanged.Invoke(this, new PropertyChangedEventArgs(propName));
    }
}

Let’s say I now have two instances of my classes, one as myParent, and the other as myChild.
I have two visual elements, that each have a property bound to the IsChnaged property of my instances; ElementA bound to myParent.IsChanged and ElementB bound to myChild.IsChanged.

When myChild.Value differs from its default value, the myChild.IsChanged is set to true and the ElementB is updated accordingly.

What I need is when either of the myParent children (which here is only one) have their IsChanged value set to true, its own (the parent’s) IsChanged value be set to true and its corresponding
element (ElementA here) be updated accordingly.

The myParent.IsChanged is only read once (when the binding is set) and it has no sense about its children changing. Where should i put the RaiseChanged(“IsChanged”) for MyParent? How can I let the parent know when its children have changed?

Thanks in advance

  • 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-23T15:24:14+00:00Added an answer on May 23, 2026 at 3:24 pm

    INotifyPropertyChanged has already provided the mechanism for you: the PropertyChanged event. Just have the parent add a handler to its children’s PropertyChanged, and then in that handler call RaiseChanged("IsChanged");

    Also, you may want to put the INotifyPropertyChanged implementation in a base class, and have your (what appear to be) ViewModels inherit from that. Not required for this option, of course, but it will make the code a little cleaner.

    Update: In the parent object:

    // This list tracks the handlers, so you can
    // remove them if you're no longer interested in receiving notifications.
    //  It can be ommitted if you prefer.
    List<EventHandler<PropertyChangedEventArgs>> changedHandlers =
        new List<EventHandler<PropertyChangedEventArgs>>();
    
    // Call this method to add children to the parent
    public void AddChild(MyChild newChild)
    {
        // Omitted: error checking, and ensuring newChild isn't already in the list
        this.MyChildren.Add(newChild);
        EventHandler<PropertyChangedEventArgs> eh =
            new EventHandler<PropertyChangedEventArgs>(ChildChanged);
        newChild.PropertyChanged += eh;
        this.changedHandlers.Add(eh);
    }
    
    public void ChildChanged(object sender, PropertyChangedEventArgs e)
    {
        MyChild child = sender as MyChild;
        if (this.MyChildren.Contains(child))
        {
            RaiseChanged("IsChanged");
        }
    }
    

    You don’t actually have to add anything to the child class, since it is already raising the correct event when it changes.

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

Sidebar

Related Questions

take a look at this example code: public class Comment { private Comment() {
Best explained with code I think, this is just a simple example: public class
I have this example code: #include template<class T> class Class { public: typedef boost::shared_ptr<Class<T>
I see a lot of example code for C# classes that does this: public
for example this code var html = <p>This text is <a href=#> good</a></p>; var
This is an example code from the prototype site. var url = '/proxy?url=' +
I suppose that this is an interesting code example. We have a class --
I'll attempt to shorten this code example: public interface IThing { //... Stuff }
i have this example: class One { public void testOne(){System.out.println(One!!!);} public void testTwo(){System.out.println(One!!!);} }
I was browsing over the following code example: public class GenericTest { public static

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.