I am currently trying to update a text box in my UI, through a backgroundworker class. This backgroundworker is in my ViewModel section of my solution, and it only occurs when my application receives data (triggered as an event on my Model).
I have used binding in my MainWindow xaml file to bind the text to properties in my ViewModel, and when breakpoints are set it appears to store the correct values, even though they are still not displayed.
I am not too sure about the split between the DoWork event & the RunWorkerCompleted event. I have the DoWork event setting the data packet to whatever the latest packet received is, and on completion (RunWorkerCompleted), I am assigning the value to the property created in the ViewModel. Have I implemented the backgroundworker class correctly?
Any help on this would greatly be appreciated.
View
<TextBox Height="23" HorizontalAlignment="Left" Margin="577,70,0,0" Name="receiveVariableValueBox" VerticalAlignment="Top" Width="120" Text="{Binding Path=RxVariableValue}"/>
ViewModel
public class DataPointsViewModel : INotifyPropertyChanged
{
public Model model;
BackgroundWorker receiveBackground;
RRTDataPacket rxDataPacket = new RRTDataPacket();
private int rxVariableValue;
public int RxVariableValue
{
get { return rxVariableValue; }
set
{
if(rxVariableValue != value)
{
rxVariableValue = value;
OnPropertyChanged(rxMajorVersion.ToString());
}
}
}
public void receivingUpdater(object sender, EventArgs e)
{
receiveBackground = new BackgroundWorker();
receiveBackground.DoWork += new DoWorkEventHandler(receiveBackground_DoWork);
receiveBackground.RunWorkerCompleted += new RunWorkerCompletedEventHandler(receiveBackground_RunWorkerCompleted);
receiveBackground.RunWorkerAsync();
}
public void receiveBackground_DoWork(object sender, DoWorkEventArgs e)
{
try
{
if (rxDataPacket != Engine.latestRxDataPacket)
{
rxDataPacket = Engine.latestRxDataPacket;
}
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
}
public void receiveBackground_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
if(rxDataPacket != null)
{
RxMajorVersion = (int)rxDataPacket.majorVersion;
RxVariableValue = (int)rxDataPacket.variableValue;
}
}