0

I'm working on project where I'm using MVVM pattern.

I have one text box:

<TextBox Height="23" HorizontalAlignment="Right" Margin="0,30,72,0" Name="textBox1" 
         Text="{Binding Path=Rspchange, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, NotifyOnValidationError=True}" VerticalAlignment="Top" Width="75" />

and label:

<Label Content="{Binding Path=LF, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Height="24" HorizontalAlignment="Left" Margin="540,3,0,0" Name="lbLF" VerticalAlignment="Top" Width="84" />

Here I have used two way binding with property Rspchange and LF respectively.

public UInt16 Rspchange
{
    get
    {
        return this.eObj.GetRsBaseline(this.index);
    }
    set
    {
        this.eObj.SetRsBaseline(value, this.Index);
        this.OnPropertyChanged(new PropertyChangedEventArgs("Rspchange"));
        this.eObj.writedata(DefaultFileName);
    }
} 

public string LF
{
    get
    {
        return this.eObj.LFrequency;
    }
    set
    {
        this.eObj.LFrequency = value;
        this.OnPropertyChanged(new PropertyChangedEventArgs(" LF"));
    }
}

On model side I have this proerty like this:

public override string LFrequency
{
    get { return configObject.lnFrequency.ToString(); }
    set { configObject.lnFrequency = Convert.ToByte(value); }
}

public override UInt16 GetRsBaseline(int channelIndex)
{
    return (byte)this.configObject.CConfig[index].Bline;
}

public override void SetRsBaseline(UInt16 value, int index)
{
    byte[] arr = BitConverter.GetBytes(value);
    this.configObject.CConfig[index].Bline = arr[0];
}

public byte Bline
{
    get { return this.bline; }
    set { this.bline = value; }
}

public byte lnFrequency
{
    get { return this.ln_frequency; }
    set { this.ln_frequency = value; }
}

My application is communicating through serial port to other application. I'm getting data from other application and based on that I have to update my GUI as well.

When I debugged my code I have seen that I'm getting updated value from other application to ln_frequency and bline but it's not updating my GUI.

Please tell me what is problem with my code so that It can update my GUI once I get data from other application.

P.S: code of writexmldata

public void WriteXmlData(string path)
{
    if ((ie.Current as XElement).Attribute("name").Value == "Bline")
    {
        (ie.Current as XElement).Attribute("value").Value = this.channel_config[0].Bline.ToString();
    }

    if ((ie.Current as XElement).Attribute("name").Value == "LFrequency")
    {
        (ie.Current as XElement).Attribute("value").Value = this.lnFrequency.ToString();
    }
}

Here my intention is to save current value of Bline and LFrequency, so that when user will launch my application again user will get latest value on GUI all time.

icebat
  • 4,696
  • 4
  • 22
  • 36
user2932395
  • 427
  • 1
  • 5
  • 21

1 Answers1

1

Ok then, you have your xaml View, a ViewModel containing Rspchange and LF properties, and a model containing LFrequency, Bline and lnFrequency properties.

You understand that an update on Rspchange will update your UI, because of the line:

this.OnPropertyChanged(new PropertyChangedEventArgs("Rspchange"));

It basically says to the UI: "Hey if you care, I update my Rspchange property". As there is the 2 way binding, the UI read the new value from your viewModel.

Now it is clear here that if you update lnFrequency or Bline, nothing is updated, as there is no notification in their setter.

I think you jump a few step in the implementation of MVVM, and this article should help you to improve it: WPF MVVM INotifyPropertyChanged Implementation - Model or ViewModel

In conclusion, what you need is a way for your model to inform your ViewModel that it was updated. But it would be more MVVM compliant to not use INotifyPropertyChanged to do it. So you could either consider that lnFrequency and Bline are actually part of your viewModel, or create a new ViewModel for them, or use any kind of classic event to spread the information "model updated by external system" around your solution.

Community
  • 1
  • 1
Ouarzy
  • 3,015
  • 1
  • 16
  • 19