绑定和INotifyPropertyChanged

时间:2011-12-08 19:39:23

标签: c# wpf

我正在尝试使用INotifyPropertyChanged事件绑定TextBlock。但它没有更新TextBlock的任何内容。 TextBlock为空。我的目标是更新显示在不同行中的项目的状态。我需要根据状态更新TextBlock的文字和颜色。

有人能告诉我我的代码有什么问题吗?

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);
    }
}

1 个答案:

答案 0 :(得分:2)

嗯,这里的具体问题是你将TextBlock绑定到Item而不是ItemStatus。但是你也在努力做事,你真的应该在XAML中做绑定细节。从视图模型中公开ItemStatus的集合,并将ListBox或其ItemsSource绑定到集合。然后你需要一个DataTemplate来定义TextBlock和ItemStatus的绑定。

Here's a good walkthrough for it in general