Reputation: 11721
My Model is as below .
public class FItem
{
public FItem()
{
FSubsystems = new BindingList<FSubsystem>();
}
public int RecordId { get; set; }
public string ItemName { get; set; }
public BindingList<FSubsystem> FSubsystems { get; set; }
}
public class FSubsystem
{
public int SubSystemID { get; set; }
public string ItemName { get; set; }
public int YearID { get; set; }
}
Code :
FItems = new ObservableCollection<MarketRecord.FItem>();
FItems.CollectionChanged += OnUiCollectionChanged;
private void OnUiCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
{
if (e.Action == NotifyCollectionChangedAction.Add)
{
var addedItem = e.NewItems[0] as MarketRecord.FItem;
if (addedItem != null)
{
addedItem.PropertyChanged += OnColumnModified;
if (addedItem.RecordId == 0)
modifedItems.Add(addedItem);
addedItem.FSubsystems.ListChanged += OnColumnModified;
}
}
}
private void OnColumnModified(object sender, EventArgs e)
{
if (sender.GetType().Name == "FItem")
{
MarketRecord.FItem modifiedItem = (sender as MarketRecord.FItem);
if (!modifedItems.Contains(modifiedItem))
modifedItems.Add(modifiedItem);
}
else
{
// add parent of the binding list (i.e fitem object ) add it to modifedItems
// Find parent here
}
}
As my code tells I need to find the parent of binding list in else clause of OnColumnModified . How can I do this ?
Upvotes: 0
Views: 101
Reputation: 174329
The way you currently structured your code this is not really possible.
You can achieve this with the help of an anonymous method:
addedItem.FSubsystems.ListChanged += (s, e) => OnColumnModified(addedItem, e);
This will register an anonymous method as the event handler for ListChanged
. When that event is raised, it discards the sender
argument of the event and instead passes in addedItem
, your FItem
instance.
You could improve the type safety of this code a bit by changing OnColumnModified
to this:
private void OnColumnModified(MarketRecord.FItem modifieditem)
{
if(!modifiedItems.Contains(modifiedItem))
modifiedItems.Add(modifiedItem);
}
The event registrations would now look like this:
addedItem.PropertyChanged += (s, e) => OnColumnModified(addedItem);
addedItem.FSubsystems.ListChanged += (s, e) => OnColumnModified(addedItem);
Upvotes: 2