I have a XamDataGrid bound to an ObservableCollection in my ViewModel:
public ObservableCollection<InventurPos> LoadedPositions { get; set; }
XAML:
<custom:XamDataGrid Name="InvPosDisplay" Grid.Row="2" Grid.Column="2" DataSource="{Binding LoadedPositions}"> <custom:XamDataGrid.FieldLayoutSettings> <custom:FieldLayoutSettings AutoGenerateFields="False"/> </custom:XamDataGrid.FieldLayoutSettings> <custom:XamDataGrid.FieldSettings> <custom:FieldSettings AllowRecordFiltering="True" LabelClickAction=dNotificationsActive="True"/> </custom:XamDataGrid.FieldSettings> <custom:XamDataGrid.ViewSettings> <custom:GridViewSettings /> </custom:XamDataGrid.ViewSettings> <custom:XamDataGrid.FieldLayouts> ... </custom:XamDataGrid.FieldLayouts> </custom:XamDataGrid>
I already managed to do this in the Code behind, using the Eventhandler OnRecordUpdated, however since i have to process some more extensive Logic to fill some Properties that are not editable by the User, i'd prefer having this Logic in my Viewmodel. Is there any Way to get the modified row(s) from the ViewModel?
(I already tried registering an OnCollectionChanged Handler to my ObservableCollection, this Changed however are not getting picked up, probably due to not raising a PropertyChangedEvent)
Hello Greisinger,
Thank you for contacting Infragistics. Are you looking to raise a CollectionChanged event when a single property, aka cell, is updated in a row? From my understanding, CollectionChanged is raised only when a row is added, or deleted for example, which is why PropertyChanged event exists. I recommend that you raise the PropertyChanged event to keep track of changes on a cell by cell basis.
I attached a sample that demonstrates the PropertyChanged event working as expected. Alternatively you could use a customized version of an ObservableCollection that keeps track of when the entire collection is modified each time a cell is modified, but at the cost of possible unknown performance. I don't recommend this approach because the property changed event is more performant.
The sample attached below is setup to trap each time a cell is exits edit mode. Two messages are fired, one for when the collectionchanged is handled within the ViewModel and another when the CollectionChanged is raised on the collection instance the grid is bound to.
Let me know if you have any questions.
XamDataGridUpdate.zip
Thank you for your Reply.
Your Suggestion with the PropertyChangedEvent gave me the right Idea.
I now use the CollectionChangedEvent to get notified each time an object is added or removed from my ObservableCollection. When this happens i Register a new PropertyChangedEventhandler on the added Object to fill the not editable fields of the Object from the Viewmodel.
LoadedPositions.CollectionChanged += PositionAddedOrRemoved; private void PositionAddedOrRemoved(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e) { if (e.Action == System.Collections.Specialized.NotifyCollectionChangedAction.Add) { foreach(var a in e.NewItems) { (a as InventurPos).PropertyChanged += RowChanged; } } if (e.Action == System.Collections.Specialized.NotifyCollectionChangedAction.Remove) { foreach (var a in e.OldItems) { (a as InventurPos).PropertyChanged -= RowChanged; } } }
The Evenhandler temporarily disables itself to Prevent a Loop.
private void RowChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e) { var handledRow = sender as InventurPos; handledRow.PropertyChanged -= RowChanged; handledRow.INV_DAT = DateTime.Now.ToString("yyyyMMddHHmmss"); handledRow.ChangedNow = true; handledRow.Changed = true; handledRow.LastChange = DateTime.Now.ToString("yyyyMMddHHmmss"); handledRow.PropertyChanged += RowChanged; }
The DataGrid itself will still get notified by the Event from my Domainobject.
Now everything is working as i wanted.
Dear Greisinger,
Thank you for following up! I am glad to hear I was able to point you in the right direction. Let me know if you have any additional questions.