1

I'm using MVVM pattern with WPF application. Data I'm getting from device which is communicating with my application (Desktop) via USB communication.

Here is my MVVM structure:

In Model: I'm getting data from device (which run on WinCE plateform) Here I have created an event public event EventHandler<PropertyEventArgs> propertyChanged;

protected void RaisepropertyChangedEvent(PropertyEventArgs e)
{
    if (this.propertyChanged != null)
    {
        this.propertyChanged(this, e);
    }
}

public class PropertyEventArgs : EventArgs
{
    public int lf { get; set; } 
    public int dateformat { get; set; }
    public string pid { get; set; }
    public int language { get; set; }
    public int Line { get; set; }
    public string Label { get; set;}
    public int uValue { get; set; }
    public int lvalue { get; set; }
    public int lMethod { get; set; }
}

In View Model I'm raising this event:

When some data is coming, then I'm raising this event:

this.RaisepropertyChangedEvent(this.pargs);

In UI: I have subscribed this event to Update UI

this.eObj.propertyChanged += new EventHandler<PropertyEventArgs>(this.EpropertyChanged); 

private void EpropertyChanged(object sender, PropertyEventArgs e)
{
    this.OnPropertyChanged(new PropertyChangedEventArgs("linePercentage"));
    this.OnPropertyChanged(new PropertyChangedEventArgs("Label"));
    this.OnPropertyChanged(new PropertyChangedEventArgs("UL"));
    this.OnPropertyChanged(new PropertyChangedEventArgs("LMethod"));
}

eobj is place where Eventargs I have declared.

Problem which I'm facing is that when value of property change, all the property is getting called.

Because of that sometime wrong value is getting set for some parameter.

Please help me to solve this problem.

currarpickt
  • 2,290
  • 4
  • 24
  • 39
punter
  • 11
  • 3
  • Each time a property changes you receive all the information? Why not store a cache state of the device and then compare the new state and the old to know what to notify? – nkoniishvt Jul 07 '16 at 10:20

1 Answers1

2

If I understand what you want you could cache the information to know what changed:

private PropertyEventArgs _oldState = null;

private void EpropertyChanged(object sender, PropertyEventArgs e)
{
    if(_oldState == null || e.SomeProperty.CompareTo(linePercentage) != 0) 
        this.OnPropertyChanged(new PropertyChangedEventArgs("linePercentage"));
    if(_oldState == null || e.Label.CompareTo(Label) != 0) 
        this.OnPropertyChanged(new PropertyChangedEventArgs("Label"));
    if(_oldState == null || e.SomeProperty.CompareTo(UL) != 0) 
        this.OnPropertyChanged(new PropertyChangedEventArgs("UL"));
    if(_oldState == null || e.lMethod.CompareTo(LMethod) != 0) 
        this.OnPropertyChanged(new PropertyChangedEventArgs("LMethod"));
    ...
    _oldState = e;
}
nkoniishvt
  • 2,442
  • 1
  • 14
  • 29