1

I am building a C# application using the Entity framework.

I have an Entity which was created from my database which has fields "Price" and "Quantity".

Using a partial class declaration I created a custom property "SubTotal" as follows:

partial class Detail {
  public decimal Subtotal {
    get
    {
      return Price * Quantity;
    }
  }  
}

The thing is, I use a lot of DataBinding in my application.

Somewhere, I use an IBindingList<Detail> as ItemsSource for a ListView.

When I modify (using code-behind) some elements of the list, the quantity and price, the listview is updated properly.

However, the Subtotal is not updated.

I think that it's because something is done automatically by the entity framework to notify that something has been changed, but I don't know what to add to my custom property so that it behaves the same way.

Could you help me out?

2 Answers 2

2

The UI doesn't know that the value of Subtotal is changed. Implement System.ComponentModel.INotifyPropertyChanged, and then raise the PropertyChanged event to let the UI know that Subtotal has changed when either Price or Quantity has changed.

For example:

partial class Detail : INotifyPropertyChanged {

   public decimal Subtotal 
   {
      get
      {
         return Price * Quantity;
      }
   }  

   public event PropertyChangedEventHandler PropertyChanged;

   private void NotifyPropertyChanged(String info)
   {
      if (PropertyChanged != null)
      {
          PropertyChanged(this, new PropertyChangedEventArgs(info));
      }
   }
}

Then when Price or Quantity is changed, you'd call NotifyPropertyChanged("Subtotal"), and the UI should update the displayed value of Subtotal appropriately.

Sign up to request clarification or add additional context in comments.

3 Comments

Well, the class EntityObject already implement INotifyPropertyChanged. It the auto-generated code-behind, they use ReportPropertyChange(...) for notifications. However, if I use ReportPropertyChanged("SubTotalTL"), I get and exception "The property 'SubTotalTL' does not have a valid entity mapping on the entity object."
@JSMaga Well, from the code you've posted, SubTotalTL doesn't exist in your Detail class, so it makes sense that you'd get an exception. What happens when you call NotifyPropertyChanged("Subtotal")?
Arf, I just had to use PropertyChanged("SubTotalTL"). I was looking for NotifyPropertyChanged. It works now. Sorry I should have come up with that myself.
2

What worked for me is to call the OnPropertyChanged method:

OnPropertyChanged("CustomPropertyName")

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.