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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T11:46:10+00:00 2026-05-15T11:46:10+00:00

This problem could be bad class design or ignorance – please bear with me:

  • 0

This problem could be bad class design or ignorance – please bear with me:

I have 2 classes – Chip (which implements INotifyPropertyChanged and represents a single poker chip) and ChipSet, which implements INotifyPropertyChanged and has an ObservableCollection of Chip.

I have a Datagrid which is bound to the Chip ObservableCollection and a Textblock which is bound to ChipSet.

ie. gridChips.ItemsSource = chipset.Chips;

The Chip class has 3 properties (for simplicity) – NumberPerPlayer, ChipValue and TotalValuePerPlayer. TotalValuePerPlayer does not have a property set or associated private member variable like the other 2 – it is dynamically based off the product of ChipValue and NumberPerPlayer.

The grid binds to all 3 of these values and shows these 3 columns. Only the first 2 are editable and the 3rd one updates as the other 2 changes.

This works fine so far – I found that in order to get the TotalValuePerPlayer column to update if either of the other columns updated I had to add this field to PropertyChangedEventArgs (see code below).

My first question- Is this the best way of updating bound class fields that are based off other fields and do not change in the UI (you cannot edit TotalValuePerPlayer directly).

public int NumberPerPlayer
{
    get { return numberPerPlayer; }
    set
    {
        if (numberPerPlayer != value)
        {
            numberPerPlayer = value;
            OnPropertyChanged("NumberPerPlayer");
            OnPropertyChanged("TotalValuePerPlayer");
        }
    }
}

public decimal ChipValue
{
    get { return chipValue; }
    set
    {
        if (chipValue != value)
        {
            chipValue = value;
            //all columns that are based on this need to be updated
            OnPropertyChanged("ChipValue");
            OnPropertyChanged("TotalValuePerPlayer");
    }
}

public decimal TotalValuePerPlayer
{
get { return chipValue * numberPerPlayer; }
}

public void OnPropertyChanged(string propertyName)
{
    if (PropertyChanged != null)
    {
        PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
    }
}

My 2nd, main question is this: I then have a label which shows the total of all the TotalValuePerPlayer totals (stupidly called TotalTotalValuePerPlayer). I put this in the ChipSet class like this (it iterates through the ObservableCollection and sums the totals):

1            public decimal TotalTotalValuePerPlayer
2            {
3                get { 
4          decimal totalTotalValuePerPlayer = 0;
5                 foreach (Chip chip in chips)
6                  {
7                      totalTotalValuePerPlayer += chip.TotalValuePerPlayer;
8                  }
9                  return totalTotalValuePerPlayer;
10     }
11           }

So – the problem is that when either of the 2 columns (NumberPerPlayer or ChipValue) this field is based on in the UI changes, it does not update.

How do I tell the parent class – ChipSet, which has the TotalTotalValuePerPlayer member to be updated when one of its children class (Chip) members in it’s ObservableCollection is updated?

If the TotalTotalValuePerPlayer was in the Chip class I could just notify it when the fields it was based off changed, but it is in the class above it?

Thanks for any advice!

Rodney

  • 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-15T11:46:11+00:00Added an answer on May 15, 2026 at 11:46 am

    Hi there I believe you are going around this the correct way you just need to go a step further. Here are how would expect the classes to look:

    Chip

    public class Chip : INotifyPropertyChanged
    {
        private int numberPerPlayer;
        public int NumberPerPlayer
        {
            get { return numberPerPlayer; }
            set
            {
                if (numberPerPlayer != value)
                {
                    numberPerPlayer = value;
                    OnPropertyChanged("NumberPerPlayer");
                    OnPropertyChanged("TotalValuePerPlayer");
                }
            }
        }
    
        private decimal chipValue;
        public decimal ChipValue
        {
            get { return chipValue; }
            set
            {
                if (chipValue != value)
                {
                    chipValue = value;
                    //all columns that are based on this need to be updated
                    OnPropertyChanged("ChipValue");
                    OnPropertyChanged("TotalValuePerPlayer");
                }
            }
        }
    
        public decimal TotalValuePerPlayer 
        { 
            get
            { 
                return chipValue * numberPerPlayer; 
            } 
        }
    
        public event PropertyChangedEventHandler PropertyChanged;
    
        public void OnPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    }
    

    ChipSet

    public class ChipSet : INotifyPropertyChanged
    {
        public ChipSet()
        {
            foreach (var chip in Chips)
            {
                chip.PropertyChanged += (s, e) => { OnPropertyChanged("TotalTotalValuePerPlayer"); };
            }
        }
    
    
        public ObservableCollection<Chip> Chips { get; set; }
    
        public decimal TotalTotalValuePerPlayer
        {
            get
            {
                return Chips.Sum(x => x.TotalValuePerPlayer);
            }
        }
    
        public void OnPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    
        public event PropertyChangedEventHandler PropertyChanged;
    }
    

    I have not tested this fully but it should point you in the right direction. Hope it helps.

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

Sidebar

Ask A Question

Stats

  • Questions 543k
  • Answers 543k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer It's mostly a question of convention. Methods with empty parameter… May 17, 2026 at 6:43 am
  • Editorial Team
    Editorial Team added an answer I don't think you will run into any particular problems… May 17, 2026 at 6:43 am
  • Editorial Team
    Editorial Team added an answer It is very easy to search for letters at the… May 17, 2026 at 6:43 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

Consider this problem: I have a program which should fetch (let's say) 100 records
Lets say I have a class which routes messages to their handlers. This class
How do I get past this variable initialization problem? If I only could figure
This is the one thing I could never get to work. My problem is
This problem has been kicking my butt for a few days now. I have
A typical problem in OO programming is the diamond problem. I have parent class
This is issue about LANGUAGE DESIGN. Please do not answer to the question until
I have a library with an API much like this: public class Library :
This may be a very simple problem, but I couldn't find an answer googleing
This problem crops up every now and then at work. Our build machine can

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.