2011-05-10 146 views
1

我不認爲我的問題已經落入其他任何人,因此希望有人能夠幫助我。WPF綁定到TextBlock不會立即更新目標

我有一個使用INotifyPropertyChnaged設置的TextBlock綁定,它可以工作。我遇到的問題是,當它更新目標控制(TextBlock的)

一個快速運行的我的代碼下來

namespace XYZ 
{ 
    public partial class Loading : Window 
    { 
     StatusMessage msg = StatusMessage.GetInstance(); 

     public loading() 
     { 
      InitializeComponent(); 
      this.DataContext = msg; 
     } 

     private void LoadBackEndUsers() 
     { 

      msg.Status = "Loading Users"; 

      //txtStatus.GetBindingExpression(TextBlock.TextProperty).UpdateTarget(); 
      //lblLoading.Focus(); 
      this.txtStatus.DataContext = msg; 

      beUsers = new BackendUsers(Database); 
      allBEUsers = beUsers.GetAllUsers(); 
     } 

     private void LoadProducts() 
     { 
      msg.Status = "Loading Products"; 

      //txtStatus.GetBindingExpression(TextBlock.TextProperty).UpdateTarget(); 
      //lblLoading.Focus(); 
      this.txtStatus.DataContext = msg; 

      products = new Product(Database); 
      allProducts = products.GetAllProducts(); 
     } 

     private void Window_ContentRendered(object sender, EventArgs e) 
     {   
      LoadBackEndUsers();   
      LoadProducts(); 
     } 
    } 
} 

現在我的問題是,我的文字塊顯示「加載產品」唯一的方法之後LoadProducts()完成。它根本不顯示「加載用戶」,因此目標只在所有內容完成後才更新。

如何讓它立即更新。註釋掉的部分是我只是嘗試各種事情來嘗試強制更新。

任何幫助將不勝感激。

親切的問候,

奧尼爾

+0

不,這將解決您的問題,但如果你的結合是真正正常工作,我不認爲你應該有重置每次更改消息時,this.txtStatus的DataContext。 – Scott 2011-05-10 14:38:31

+0

是的,實際上只是用它來試圖強制更新測試 – Neill 2011-05-11 09:18:28

回答

3

的問題是,你的數據的檢索是同一個線程的UI邏輯上發生。這意味着,即使您更改了屬性值並引發了OnPropertyChanged,也只有在阻止數據訪問完成後纔會重新評估它。相反,你應該使用BackgroundWorker。下面是通過你如何使用這個走了一大篇:

http://elegantcode.com/2009/07/03/wpf-multithreading-using-the-backgroundworker-and-reporting-the-progress-to-the-ui/

+0

tx馬特,我覺得這是類似的東西,將檢查出的鏈接,看看我可以拿出 – Neill 2011-05-10 20:32:43

+0

嗨馬特,發送該鏈接。與後臺工作人員一起工作,因爲我對發生的事情有更多的控制權。工作非常好。 – Neill 2011-05-11 11:13:43

2

StatusMessage類應該實現 INotifyPropertyChanged

編輯:Im相當肯定,你的Window_ContentRendered eventhanler塊每一個UI更新。我寫了對我的作品有點樣品:

public partial class MainWindow : Window 
{ 
    StatusMessage msg = new StatusMessage(); 
    public MainWindow() 
    { 
    InitializeComponent(); 
    this.DataContext = msg; 
    } 

    private void LoadBackEndUsers() 
    { 
    Task.Factory.StartNew(() => 
     { 
     this.Dispatcher.BeginInvoke(new ThreadStart(() => msg.Status = "Loading Users"), DispatcherPriority.Normal); 
     //Load users here: 
     Thread.Sleep(2000); 
     this.Dispatcher.BeginInvoke(new ThreadStart(() => msg.Status = "Users loaded"), DispatcherPriority.Normal); 

     // If users loaded start load products: 
     LoadProducts(); 
     }); 
    } 

    private void LoadProducts() 
    { 
    Task.Factory.StartNew(() => 
    { 
     this.Dispatcher.BeginInvoke(new ThreadStart(() => msg.Status = "Loading Products"), DispatcherPriority.Normal); 
     //Load products here: 
     Thread.Sleep(2000); 
     this.Dispatcher.BeginInvoke(new ThreadStart(() => msg.Status = "Products Loaded"), DispatcherPriority.Normal); 
    }); 
    } 

    private void Window_ContentRendered(object sender, EventArgs e) 
    { 
    LoadBackEndUsers(); 
    //LoadProducts(); 
    } 
} 
+0

「我有一個使用INotifyPropertyChnaged設置TextBlock綁定,它確實工作。我遇到的問題是當它更新目標控件(TextBlock)」 - 我確實說過這個,我確實提到了綁定的工作,我只是不知道爲什麼它不會立即更新後msg.Status =「blah」; – Neill 2011-05-10 14:26:47

+0

@尼爾好吧,我需要去看醫生檢查我的眼睛。 :D對不起。 – Ben 2011-05-10 14:29:16

+0

沒有probs,併爲我的不穩定的迴應感到抱歉,這只是讓我瘋狂! – Neill 2011-05-10 14:31:47