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>