2011-05-10 44 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