n00bAppDev
n00bAppDev

Reputation: 630

WPF: UI Element is NOT Updating

I have a BackgroundWorker and within that worker I am reading data from an excel file. If there is an error in the excel file, the worker completes and then presents another form where the user can input the correction and then press Ok with then runs the worker again from the beginning. When the worker completes successfully it is supposed to update a label on my Mainwindow to say it has loaded the excel. But the label WILL NOT update. When I debug it I can see the code to update the label runs, but it simply does not work. Please help, this is driving me insane!

Here is my code.

    private void worker_ReadFileData(object sender, DoWorkEventArgs e) {

        for (int j = 1; j < rcount + 1; j++) {
            worker.ReportProgress(j);

            // Do work


            if (j == 1) {
                ColumnIndex column = this.ValidateColumnIndexes(tableType);
                if (column != null) {    // If error in file, complete worker
                    fileData.isDataLoaded = false;
                    e.Result = fileData;
                    return;
                }
            }
        }

        fileData.isDataLoaded = true;
        e.Result = fileData;            // Pass the data to the completed method.

    }


    private void worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) {
        if (e.Error != null) {
        } else if (e.Cancelled) {
        } else {

            FileData fileData = (FileData) e.Result;

            if (fileData.isDataLoaded == true) {
                testLabel.Content = "It works!";
            } else {
                // Show Dialog where user can input the correction
                ColumnIndexPrompt columnIndexPrompt = new ColumnIndexPrompt(fileData.FilePath, fileData.FileExtension, fileData.TableType, fileData.Column);
                columnIndexPrompt.ShowDialog();
            }
        }
    }

    public void TriggerReadDataFile(string filePath, string fileExt, int tableType) {
        progBar.Value = 0;
        // Read the file data and populate the Registrars list, then show the datagrid
        worker.RunWorkerAsync(new FileData(filePath, fileExt, tableType));
    }

EDIT: Here is the code from the second Window that I open (using .ShowDialog() in the code above)

    public ColumnIndexPrompt(string filePath, string fileExt, int tableType, ColumnIndex column) {
        InitializeComponent();

        this.filePath = filePath;
        this.fileExt = fileExt;
        this.tableType = tableType;
        this.column = column;

        lblColumnIndexErrorMsg.Text = column.ErrorMsg;
    }

    private void btnColumnIndexApply_Click(object sender, RoutedEventArgs e) {
        MainWindow originalForm = new MainWindow();
        int correctColumnNumber = int.Parse(txtColumnIndexCorrection.Text);
        column.Index = correctColumnNumber - 1;
        originalForm.UpdateSingleColumnIndex(column);
        originalForm.TriggerReadDataFile(this.filePath, this.fileExt, this.tableType);
        this.Close();
    }

Upvotes: 1

Views: 95

Answers (1)

tabby
tabby

Reputation: 1918

You are creating MainWindow object again just change it to:

MainWindow mainWindow =  Application.Current.MainWindow;

Or

MainWindow mainWindow = Application.Current.Windows.OfType<MainWindow>().FirstOrDefault();

Here:

private void btnColumnIndexApply_Click(object sender, RoutedEventArgs e) 
{
    MainWindow originalForm = (MainWindow)Application.Current.MainWindow; //here
    int correctColumnNumber = int.Parse(txtColumnIndexCorrection.Text);
    column.Index = correctColumnNumber - 1;
    originalForm.UpdateSingleColumnIndex(column);
    originalForm.TriggerReadDataFile(this.filePath, this.fileExt, this.tableType);
    this.Close();
}

Upvotes: 3

Related Questions