2

So I have a class with 40 or so properties that are updated from communication with a micro controller. This class implements INotifyPropertyChanged.

Loose Example:

private int _Example;
public int Example
{
    get
    {
        return _Example;
    }
    set
    {
        _Example = value;
        OnPropertyChange("Example");
    }
}

And the OnPropertyChange function:

protected void OnPropertyChange(string p_Property)
{
    if (PropertyChanged != null)
    {
        PropertyChanged(this, new PropertyChangedEventArgs(p_Property));
    }
}

public event PropertyChangedEventHandler PropertyChanged;

Binding (many of these)

Second_Class_Control.DataBindings.Clear();
Second_Class_Control.DataBindings.Add("My_Property",  FirstClass,    "Example");

In the main form I've set up binds to display and react to these values. One of those happens to land on another property in a another class. I happened to place a breakpoint in the set function of this property, and noticed it was being called any time any property from the first class changed.

Is this the correct behavior? I don't notice any performance hits but I plan on having many instances of these classes running together and wasn't expecting this.

Thanks

plast1k
  • 793
  • 8
  • 15
  • Are you absolutely sure you aren't setting this property in the event handler for PropertyChanged? What does your stack trace look like? – Wonko the Sane Oct 05 '12 at 20:37
  • This sounds really strange. Could you post more code? – Josh C. Oct 05 '12 at 20:41
  • Adding some to my post. A breakpoint before the expected call to OnPropertyChange in the FirstClass's "Example" property "Set" method is not hit. The stack trace shows a call to the OnPropertyChange method with other arbitrary properties as the parameter string depending on when it stopped. – plast1k Oct 05 '12 at 20:44
  • I've retagged assuming this is WPF (if it's Sliverlight, please correct). – Sklivvz Oct 06 '12 at 15:12
  • Seems as though this is just the behavior, at this time I am unsure of a way around it. http://stackoverflow.com/questions/2820447/net-winforms-inotifypropertychanged-updates-all-bindings-when-one-is-changed-b – plast1k Oct 16 '12 at 16:37

1 Answers1

0

Hmm.. I noticed that you have the your OnPropertyChange virtual. Why is this, are you making a override somewhere?

I usually creates it like this :

public class ViewModelBase : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

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

then for the usage :

public class MainWindowViewModel : ViewModelBase
{
    private string name;
    public string Name
    {
     get { return name; }
     set { name = value; OnPropertyChanged("Name"); }
    }
}
Jonas W
  • 3,200
  • 1
  • 31
  • 44
  • Speedy response and good catch. I'm not sure why I had it that way, though my current needs don't require it. Removing it does not seem to affect this behavior however, ill edit my post. Thanks though. – plast1k Oct 05 '12 at 20:35
  • Intressting, then i've no qlue why you are getting that issue.. How does the binding look? – Jonas W Oct 05 '12 at 20:37