Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to update a dynamic field using ObservableCollection on a Silverlight datagrid
    primarykey
    data
    text
    <p>This problem could be bad class design or ignorance - please bear with me:</p> <p>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.</p> <p>I have a Datagrid which is bound to the Chip ObservableCollection and a Textblock which is bound to ChipSet.</p> <p>ie. gridChips.ItemsSource = chipset.Chips;</p> <p>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.</p> <p>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.</p> <p>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).</p> <p>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).</p> <pre><code>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"); } } </code></pre> <p>public decimal TotalValuePerPlayer { get { return chipValue * numberPerPlayer; } }</p> <pre><code>public void OnPropertyChanged(string propertyName) { if (PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); } } </code></pre> <p>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):</p> <pre><code>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 } </code></pre> <p>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.</p> <p>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?</p> <p>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?</p> <p>Thanks for any advice!</p> <p>Rodney</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload