ItemPropertyChanged无法处理observableCollection.Why?

时间:2010-10-03 10:19:30

标签: c# wpf

我已经搜索了一个解决方案的高低,但我似乎没有找到它的底部。 像网上的很多帖子我似乎没有让我的itemPropertyChanged工作。 编辑集合中的项目时不会触发。为什么。

有点长,但这是我放在一起的一个例子。

我有一个customerViewModel,其中包含OrderViewModel的集合 在数据网格中编辑订单时,事件不会触发。

我已经实现了以下功能,但只在加载时才会被解雇。 好像它不是同一个东西的集合......

INotifyPropertyChanged inpc = OrderViewModels;
inpc.PropertyChanged += OnItemPropertyChanged; 

有什么建议吗?这让我很生气

模型

public class Order
{
    public int Id { get; set; }
    public string Description { get; set; }
    public int CustomerId{ get; set; }
}

public class Customer
{
    public Customer()
    {
        Orders=new ObservableCollection<Order>();
    }

    public int Id { get; set; }
    public string Name { get; set; }
    public string Surname{ get; set;}
    public ObservableCollection<Order> Orders{ get; set;}

}

的ViewModels

public class CustomerViewModel : ViewModelBase
{
    private Customer _customerModel;

    public CustomerViewModel(Customer customerModel)
    {
        _customerModel = customerModel;
        _orderViewModels = new ObservableCollection<OrderViewModel>();
        OrderViewModels.CollectionChanged += OnOrdersCollectionChanged;
        INotifyPropertyChanged inpc = OrderViewModels;
        inpc.PropertyChanged += OnItemPropertyChanged; 

    }

    private void OnItemPropertyChanged(object sender, PropertyChangedEventArgs e)
    {
        //not firing!!!!!!!!!!!!!!!!!!!!!!!!!
    }

    void OnOrdersCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
    {
        switch (e.Action)
        {
            case NotifyCollectionChangedAction.Add:
                _customerModel.Orders.Insert(e.NewStartingIndex, ((OrderViewModel)e.NewItems[0]).OrderModel);
                break;
            case NotifyCollectionChangedAction.Remove:
                _customerModel.Orders.RemoveAt(e.OldStartingIndex);
                break;
            case NotifyCollectionChangedAction.Replace:
                _customerModel.Orders[e.OldStartingIndex] = ((OrderViewModel)e.NewItems[0]).OrderModel;
                break;
            case NotifyCollectionChangedAction.Move:
                _customerModel.Orders.Move(e.OldStartingIndex, e.NewStartingIndex);
                break;

            default:
                throw new ArgumentOutOfRangeException();
        }
    }

    public int Id
    {
        get { return _customerModel.Id; }
        set
        {
            _customerModel.Id = value;
            OnPropertyChanged("Id");
        }
    }

    public string Name
    {
        get { return _customerModel.Name; }
        set
        {
            _customerModel.Name = value;
            OnPropertyChanged("Name");
        }
    }

    public string Surname
    {
        get { return _customerModel.Surname; }
        set
        {
            _customerModel.Surname = value;
            OnPropertyChanged("Surname");
        }
    }
    public Customer CustomerModel
    {
        get { return _customerModel; }
        set
        {
            _customerModel = value;
            OnPropertyChanged("");
        }
    }

    private ObservableCollection<OrderViewModel> _orderViewModels;

    public ObservableCollection<OrderViewModel> OrderViewModels
    {
        get { return _orderViewModels; }
        set
        {
            _orderViewModels = value;
            OnPropertyChanged("OrderViewModels");
        }
    }
}

public class OrderViewModel:ViewModelBase
{
    private Order _orderModel;

    public OrderViewModel(Order orderModel)
    {
        _orderModel = orderModel;
    }

    public int Id
    {
        get { return _orderModel.Id; }
        set
        {
            _orderModel.Id = value;
            OnPropertyChanged("Id");
        }
    }
    public int CustomerId
    {
        get { return _orderModel.CustomerId; }
        set
        {
            _orderModel.CustomerId = value;
            OnPropertyChanged("CustomerId");
        }
    }
    public string Description 
    {
        get { return _orderModel.Description; }
        set
        {
            _orderModel.Description = value;
            OnPropertyChanged("Description");
        }
    }

    public Order OrderModel
    {
        get { return _orderModel; }
        set
        {
            _orderModel = value;
            OnPropertyChanged("");
        }
    }
}

存储库

public class OrderRepository
{
    public static ObservableCollection<Order> GetOrders(int customerId)
    {
        return new ObservableCollection<Order>
               {
                   new Order {Id = 1, CustomerId=1, Description = "MotherBoard"},
                   new Order {Id = 2, CustomerId=1,Description = "Video Card"},
                   new Order {Id = 3, CustomerId=1,Description = "TV"},
                   new Order {Id = 4, CustomerId=1, Description = "Video Recorder"},
                   new Order {Id = 5, CustomerId=1,Description = "Speakers"},
                   new Order {Id = 6, CustomerId=1,Description = "Computer"}
               };
    }
}

查看

public partial class OrdersView
{
    public OrdersView()
    {
        InitializeComponent();

        if (!DesignerProperties.GetIsInDesignMode(this))
        {
            var customerVm =
                new CustomerViewModel(new Customer
                                          {
                                              Id = 1,
                                              Name = "Jo",
                                              Surname = "Bloggs"
                                          });

            var orders = OrderRepository.GetOrders(1);
            foreach (var orderModel in orders)
            {
                customerVm.OrderViewModels.Add(new OrderViewModel(orderModel));
            }
            DataContext = customerVm;
        }
    }
}

的Xaml

<Grid>
    <DataGrid AutoGenerateColumns="False" AlternationCount="2" ItemsSource="{Binding Path=OrderViewModels}">
        <DataGrid.Columns>
            <DataGridTextColumn Binding="{Binding Id}" Header="Id"/>
            <DataGridTextColumn Binding="{Binding CustomerId}" Header="Customer Id"/>
            <DataGridTextColumn Header="Description" Binding="{Binding Description,UpdateSourceTrigger=PropertyChanged}"/>
        </DataGrid.Columns>
    </DataGrid>
</Grid>

2 个答案:

答案 0 :(得分:6)

    INotifyPropertyChanged inpc = OrderViewModels;
    inpc.PropertyChanged += OnItemPropertyChanged; 

ObservableCollection<T>上的任何属性发生变化时,该代码会通知您,ObservableCollection<T>中的项目的属性发生变化时,。例如,添加或删除OrderViewModel时应调用您的处理程序,因为Count上的ObservableCollection<OrderViewModel>属性会发生变化。

没有任何内容在PropertyChanged内传播OrderViewModel事件,并将它们聚合为单个事件。当我想这样做时,我使用了一个名为ItemObservableCollection的课程:

public sealed class ItemObservableCollection<T> : ObservableCollection<T>
    where T : INotifyPropertyChanged
{
    public event EventHandler<ItemPropertyChangedEventArgs<T>> ItemPropertyChanged;

    protected override void InsertItem(int index, T item)
    {
        base.InsertItem(index, item);
        item.PropertyChanged += item_PropertyChanged;
    }

    protected override void RemoveItem(int index)
    {
        var item= this[index];
        base.RemoveItem(index);
        item.PropertyChanged -= item_PropertyChanged;
    }

    protected override void ClearItems()
    {
        foreach (var item in this)
        {
            item.PropertyChanged -= item_PropertyChanged;
        }

        base.ClearItems();
    }

    protected override void SetItem(int index, T item)
    {
        var oldItem = this[index];
        oldItem.PropertyChanged -= item_PropertyChanged;
        base.SetItem(index, item);
        item.PropertyChanged += item_PropertyChanged;
    }

    private void item_PropertyChanged(object sender, PropertyChangedEventArgs e)
    {
        OnItemPropertyChanged((T)sender, e.PropertyName);
    }

    private void OnItemPropertyChanged(T item, string propertyName)
    {
        var handler = this.ItemPropertyChanged;

        if (handler != null)
        {
             handler(this, new ItemPropertyChangedEventArgs<T>(item, propertyName));
        }
    }
}

public sealed class ItemPropertyChangedEventArgs<T> : EventArgs
{
    private readonly T _item;
    private readonly string _propertyName;

    public ItemPropertyChangedEventArgs(T item, string propertyName)
    {
        _item = item;
        _propertyName = propertyName;
    }

    public T Item
    {
        get { return _item; }
    }

    public string PropertyName
    {
        get { return _propertyName; }
    }
}

我可以这样使用它:

var orders = new ItemObservableCollection<OrderViewModel>();
orders.CollectionChanged   += OnOrdersChanged;
orders.ItemPropertyChanged += OnOrderChanged;

答案 1 :(得分:1)

System.ComponentModel.BindingList<Type>提供与ObservableCollection<Type>相同的功能,并正确处理 PropertyChanged 事件。

祝你好运