tags:

views:

47

answers:

1

Hi,

I have searched high and low for a solution but I dont seem to get to the bottom of it. Like many posts on the net I dont seem to make my itemPropertyChanged work. It does not fire when editing an item in the collection.Why.

a bit lenghty but this is an example I have put together.

I have a customerViewModel that contains a Collections of OrderViewModels when editing the order in the datagrid the event does not fire.

I have implemented the following but never gets fired when editing only when loading. As if it's not the same collection of something...

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

Any suggestions?It's driving me mad

Models
=======
 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("");
        }
    }
}

Repository
===================

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



View
======
 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>
+4  A: 
    INotifyPropertyChanged inpc = OrderViewModels;
    inpc.PropertyChanged += OnItemPropertyChanged; 

That code will notify you when any property on the ObservableCollection<T> changes, not when items in the ObservableCollection<T> have their properties changed. For example, your handler should be called when you add or remove an OrderViewModel because the Count property will change on the ObservableCollection<OrderViewModel>.

Nothing is propagating the PropertyChanged event inside OrderViewModels and aggregating them into a single event for you. I use a class I called ItemObservableCollection when I want to do this:

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

I can use it like this:

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

HTH,
Kent

Kent Boogaart
Hi,Thanks for your reply.just trying to make it work and compile.Am missing "ItemPropertyChanged.Raise" and the ItemPropertyChangedEventArgs class. Would you mind to post it so that I can try. Very grateful. I have just bumped into the ListCollectionView is that another way to achieve the same?
Nice. I'm changing all my code to use you `ItemPropertyChangedEventArgs` design. Previously I just prepended `Item.` to the property name as it bubbled up, but this offers a much better solution.
Jonathan Allen
@Kent .What does your ItemPropertyChangedEventArgs look like.When You say Raise do you mean Invoke? Thanks for your help
I too would love to know what ItemPropertyChangedEventArgs looks like... C# gives me errors on the line ItemPropertyChanged.Raise(...) because it has no idea what Raise is, and it has no idea what an ItemPropertyChangedEventArgs is.
myermian
Updated code to include `ItemPropertyChangedEventArgs`.
Kent Boogaart