2011-12-08 38 views
1

我試圖使用INotifyPropertyChanged事件綁定TextBlock。但它沒有更新任何內容到TextBlockTextBlock是空白的。我的目標是更新顯示在不同行中的項目的狀態。我需要根據狀態更新TextBlock的文字和顏色。綁定和INotifyPropertyChanged

有人能告訴我我的代碼有什麼問題嗎?

public class ItemStatus : INotifyPropertyChanged 
{ 
    string itemStatus; 
    Brush itemStatusColor; 

    public string ItemStatus 
    { 
     get { return itemStatus; } 
     set 
     { 
      itemStatus = value; 
      this.OnPropertyChanged("ItemStatus"); 
     } 
    } 

    public Brush ItemStatusColor 
    { 
     get { return itemStatusColor; } 
     set 
     { 
      itemStatusColor = value; 
      this.OnPropertyChanged("ItemStatusColor"); 
     } 
    } 

    public event PropertyChangedEventHandler PropertyChanged; 
    void OnPropertyChanged(string propName) 
    { 
     if (this.PropertyChanged != null) 
      this.PropertyChanged(
       this, new PropertyChangedEventArgs(propName)); 
    } 
} 

public class Items 
{ 
    List<ItemStatus> currentItemStatus; 

    public List<ItemStatus> CurrentItemStatus 
    { 
     get { return currentItemStatus; } 
     set { currentItemStatus = value; } 
    } 
} 

public partial class DisplayItemStatus : Page 
{ 
    .... 
    .... 

    public DisplayItemStatus() 
    { 
     foreach (Product product in lstProductList) 
     { 
      TextBlock tbItemStatus = new TextBlock(); 
      .... 

      Items objItems = new Items(); 

      Binding bindingText = new Binding(); 
      bindingText.Source = objItems; 
      bindingText.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged; 
      bindingText.Path = new PropertyPath(String.Format("ItemStatus")); 
      tbItemStatus.SetBinding(TextBlock.TextProperty, bindingText); 

      Binding bindingColor = new Binding(); 
      bindingColor.Source = objItems; 
      bindingColor.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged; 
      bindingColor.Path = new PropertyPath(String.Format("ItemStatusColor")); 
      tbItemStatus.SetBinding(TextBlock.ForegroundProperty, bindingColor); 

      grdItemsList.Children.Add(tbItemStatus); 
     } 
    } 

    private void UpdateItems_Click(object sender, MouseButtonEventArgs e) 
    { 
     int intCount = 0; 

     List<Product> ProductList = new List<Product>(); 
     List<ItemStatus> ItemList = new List<ItemStatus>(); 

     ProductList = GetProducts(); 
     foreach (Product product in ProductList) 
     { 
      intCount++; 
      UpdateStatus(intCount, ItemList); 
     } 
    } 

    public void UpdateStatus(int intIndex, List<ItemStatus> ItemList) 
    { 
     ItemStatus status = new ItemStatus(); 
     status.ItemStatus = strOperationStatus; 
     status.ItemStatusColor = brshForegroundColor; 
     ItemList.Add(status); 
    } 
} 
+0

你有一個項目列表,你正在使用TextBlock來顯示它們?爲什麼不使用ListBox? – MBen

+2

很多C#和沒有XAML ......你正在爲自己製造困難。 –

回答

2

嗯,這裏的具體問題是你將TextBlock綁定到Item而不是ItemStatus。但是,你也在以困難的方式做事,你真的應該在XAML中完成綁定細節。從視圖模型公開一個ItemStatus的集合,並且有一個ListBox或其ItemsSource綁定到集合的東西。然後你需要一個DataTemplate,它定義了TextBlock和與ItemStatus的綁定。

Here's a good walkthrough for it in general

+0

謝謝大家。我是WPF的初學者。其實我不得不跳過很多代碼。所以按照當前的邏輯,我必須動態地創建TextBlocks。我沒有別的選擇。此外,狀態正在通過後臺進程得到更新,我認爲您提供的鏈接是有幫助的。讓我試試,如果我可以按照你的建議使用DataTemplate。 – Bian

+0

非常感謝您的大使用者。你提供給我的鏈接非常有幫助。我用過DataTemplate,DataTriggers和ItemsControl。我必須在我的代碼中更改很多才能使用DataTemplate。但它對我很有幫助。 – Bian

相關問題