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

하지 않음 msg를 변경할 때마다 this.txtStatus의 DataContext. – Scott

+0

예, 실제로 테스트를 위해 업데이트를 시도하려고했습니다. – Neill

답변

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

+0

안녕 Matt, Tx를 생각해 낼 수있는 것을 보는 것에 따라 링크를 조사 할 것이다. 무슨 일이 벌어지고 있는지 더 잘 제어 할 수 있기 때문에 배경 작업자에게갔습니다. 매우 잘 작동합니다. – Neill

2

귀하의 StatusMessage 클래스는 INotifyPropertyChanged 구현해야합니다 :

편집 : 임 확신 당신의 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)을 업데이트 할 때 발생합니다."- 나는 이것을 말했고 나는 구속력있는 작품에 대해 언급했다. 나는 set msg.Status = "blah"; – Neill

+0

@Neil 알았어. 내 눈을 확인하기 위해 의사에게 갈 필요가있어. : D 죄송합니다. – Ben

+0

probs, 그리고 내 약간 휘발성 반응에 대한 미안 해요, 이건 그냥 날 미친 운전입니다! – Neill

관련 문제