簡體   English   中英

WPF:UI元素未更新

[英]WPF: UI Element is NOT Updating

我有一個BackgroundWorker,在該工作者中我正在從excel文件中讀取數據。 如果excel文件中存在錯誤,則工作人員完成,然后顯示另一個表單,用戶可以在其中輸入更正,然后按“確定”,然后從頭開始再次運行工作程序。 當工作人員成功完成時,應該更新我的Mainwindow上的標簽,說它已經加載了excel。 但標簽不會更新。 當我調試它時,我可以看到更新標簽運行的代碼,但它根本不起作用。 請幫忙,這讓我瘋了!

這是我的代碼。

    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));
    }

編輯:這是我打開的第二個窗口的代碼(在上面的代碼中使用.ShowDialog())

    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();
    }

您正在再次創建MainWindow對象,只需將其更改為:

MainWindow mainWindow =  Application.Current.MainWindow;

要么

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

這里:

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();
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM