2016-11-22 35 views
0

我的ViewModel中有一个属性,其值在BackgroundWorker的DoWork方法中更改。当我启动应用程序并单击启动BackgroundWorker的按钮时,我会看到此属性的值如何更改。但是,当我打开一个新窗口时,该属性保留其默认值,即使BackgroundWorker仍在运行,该属性也不会更新。更新WPF中另一个窗口的属性值

她是我的ViewModel代码:

private string currentData; 

... 

public ViewModel() 
    { 
     ... 

     // Property initialised with a default value 
     currentData = "BackgroundWorker is not running"; 

     ... 
    } 

public string CurrentData 
    { 
     get { return this.currentData; } 
     private set 
     { 
      if (this.currentData != value) 
      { 
       this.currentData = value; 
       this.RaisePropertyChanged("CurrentData"); 
      } 
     } 
    } 

private void DoWork(object sender, DoWorkEventArgs e) 
    { 
     isUpdating = true; 

     ... 

     this.CurrentData = "BackgroundWorker is running..."; 

     for (...) 
     { 
      ... 

      if(...) 
      { 
       this.CurrentData = "value1"; 
      } 

      else 
      { 
       this.CurrentData = "value2"; 

       ... 
      }  
     } 
    } 

RaisePropertyChanged方法:

<TextBlock Margin="10" MinWidth="250" VerticalAlignment="Center" Text="{Binding CurrentData}" FontSize="12" Foreground="White" HorizontalAlignment="Left" /> 

BackgroundWorker的:两个窗口(主窗口和newtWindow)

private void RaisePropertyChanged(string propertyName) 
    { 
     PropertyChangedEventHandler handler = PropertyChanged; 
     if (handler != null) 
     { 
      handler(this, new PropertyChangedEventArgs(propertyName)); 
     } 
    } 

XAML代码:

private readonly BackgroundWorker worker; 

... 

public ImageViewModel() 
    { 
     currentData = "BackgroundWorker is not running"; 

     this.worker = new BackgroundWorker(); 
     this.worker.DoWork += this.DoWork; 
     this.worker.ProgressChanged += this.ProgressChanged; 
     this.worker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(worker_Completeted); 
     this.worker.WorkerReportsProgress = true; 
    } 

你能告诉我做错了什么,我该如何解决它?

+0

那里有很多椭圆(...)你看不到类定义, t看到'RaisePropertyChanged'方法,你没有看到任何事件,请提供实际编译的代码,否则几乎不可能发生什么问题 – Sefe

+0

椭圆(...)只代表很多被执行的操作。所以,因为代码很长,所以是 – Guilian

+0

你有Whatevs类的实例A.你正在改变实例A的属性。如果你想要两个窗口显示这个实例A的改变版本,那么这两个窗口必须有实例A的引用。你如何在两个窗口之间共享实例取决于你的设计,谁构造窗户,以及它们是如何构建的。 – Will

回答

0

你将不得不创建一个私有字符串引用了你的财产, 的其属性可以设置的值,它会被保存在栈上, 像这样(这是多么WPF得到文本框信息中文本属性)

 private string _text; //string that is used as a reference which you can plug your new new window 

    public string Text 
    { 
     get 
     { 
      return this._text; 
     } 
     set 
     { 
      this._text = value; 
      if (null != PropertyChanged) 
      { 
       this.PropertyChanged(this, new PropertyChangedEventArgs ("Text")); 
      } 
     } 
    } 
+0

我应该如何使用新窗口的文本,因为我使用MainWindow的CurrentData? – Guilian

0

我会避免从后台线程更新绑定到UI的属性。我不确定这是否能解决您的问题,但我会尝试使用BackgroundWorker的ReportProgress方法来通知您的ViewModel有关CurrentData的更改。然后在OnProgressChanged事件处理程序中,您可以将CurrentData设置为一个新的String。

public void ReportProgress(int percentProgress, object userState) 

您可以将您的字符串放入“userState”对象。

编辑

是这样的:

public ViewModel() 
    { 
     ... 

     backgroundWorker.ReportsProgress = true; 
     backgroundWorker.ProgressChanged += OnProgressChanged; 

     ... 
    } 

private void DoWork(object sender, DoWorkEventArgs e) 
{ 
    isUpdating = true; 

    ... 

    ReportProgress(0,"BackgroundWorker is running..."); 

    for (...) 
    { 
     ... 

     if(...) 
     { 
      ReportProgress(0,"value1"); 
     } 

     else 
     { 
      ReportProgress(0,"value2"); 

      ... 
     }  
    } 
} 

private void OnProgressChanged(object sender, ProgressChangedEventArgs e) 
{ 
    this.CurrentData = (string)e.UserState; 
} 
+0

我试过了,但是porperty没有在新窗口上更新。它保留了默认值:( – Guilian

+0

)如何设置新窗口的DataContext? – mbger

+0

' ' – Guilian

0
所以从你已经表示,到目前为止我的理解如下

确定:

从你原来的问题:

但是,当我打开一个新窗口时,此属性保留其默认值,即使BackgroundWorker仍在运行,它也不会更新。

从你到我以前的答复有关设置窗口的DataContext的评论:

<Window.DataContext> <local:ViewModel /> </Window.DataContext> 

当您创建一个新的窗口,你还可以创建你的视图模型的新实例。这个新实例也有自己的BackgroundWorker。当你说 ”...即使BackgroundWorker仍在运行“,那么这只适用于您的第一个窗口,因为您的新窗口中的Backgroundworker必须先启动。

如果您希望使用相同的DataContext(以及相同的BackgroundWorker)这两个窗口,你需要将你的新窗口的DataContext设置为ViewModel的已经存在的实例

相关问题