Change DataGrid contents in the CellEditEnding event - wpf

I have bound the WPF DataGrid to an observable collection of view models, where each view model represents each row in the DataGrid. The view model handles the BeginEdit and CellEditEnding events.
In one of the scenarios, I want to change the contents of the observable collection in the CellEditEnding event. But, I cant do this because the DataGrid is still in edit mode and if I try to add / remove items from the observable collection an exception is thrown and it causes my application to crash.
Any suggestions?

Dispatch it please.
In the CellEditEnding handler invoke your code with the Dispatcher.BeginInvoke() method.
Also you said
The view model handles the BeginEdit and CellEditEnding events.
If you are using pure MVVM then this is forbidden. MVVM implements events via Delegate/Relay Commands.

Related

WPF telerik GridView aggregate is not updating until focus out of a grid view field

telerik:GridView, using aggregate function to show sum in footer, the property is being updated from somewhere else and GridView is not calling PropertyChanged until focus out from GridView Column.
How we can refresh aggregate as soon as Item-source Property is changed.
I want to get aggregate updated as soon as ItemSource Property is updated.
On GridView property change I am using
private void GV_PropertyChanged(object s, PropertyChangedEventArgs e)
{
GV.CalculateAggregate();
}
According to the docs, it seems like you should raise the CollectionChanged event for the source collection:
In order to get the aggregate results refreshed, you have to notify
the GridView that the bound collection has been changed. A
CollectionChanged notification of the bound collection should be
raised.
There are four possible solutions:
You can update the value as illustrated in this help article on how to
edit an item outside RadGridView.
Ensure that a CollectionChanged event is raised from the bound source
collection.
Invoke RadGridView.CalculateAggregates() method.
Invoke RadGridView.Rebind() method. It will raise CollectionChanged
notification with action Reset. Please note that the entire view will
be recreated.

Update WPF control when event fires

I have a class which monitors a log file. It will fire an event when a new line is added.
What is the proper way, to update multiple controls in WPF?
Keep in mind that I am new to WPF bindings.
You should learn about bindings and MVVM. In MVVM you can have your viewmodel class implement INotifyPropertyChanged allowing the view to be automatically updated when a binding property in viewmodel class is updated. In your case your viewmodel can subscribe to the fired event and update a property which in turn will update the view (controls).

Refresh a DataGrid in the MVVM pattern

I have a View with a dataGrid. This datagrid bind a property in the ViewModel that is an ObservableCollection.
I edit some data in the dataGrid, and a field is updated by code, because it depends on some operations. Well, if I check the item in the observable collection, I can see that all the data is correct, but the info in the dataGrid is no refresh.
I want to force the refresh because I know that the observableCollection only raise the change property event when I add o remove items, but not if I edit one of them.
Because I am use Entity Framework 4.1, really the ItemsSource of the dataGrid is the local of the DbSet, so I don't know how to implement the notifyPorpertyChanged in the classes of the model edmx, and I am looking for an alternative, like to force refresh the dataGrid.
Because the property of the ViewModel that I use to bing the ItemsSource of the dataGrid is a reference to the local, I mean that to set the property I do myProperty = myContext.MyTable.Local and that raise the event PropertyChanged that I implement in my ViewModel, I try to do myProperty = myContext.MyTable.Local again to try to raise the event and force the refresh of the dataGrid, but it does not work.
What alternatives do I have?
Make sure that you have the Binding Mode set to TwoWay. Implement in the set portion of your property OnPropertyChanged and the rest should take care of itself.

ObservableCollection DataGrid

I bound the ObservableCollection to the dataGrid itemssource.
the collectionChangedEvent of the observable Collection is getting called only when we add, delete, remove. But not firing when we update the record.
how to fire the event for Update too?
If you want to be notified when an item is changed (ie you want to subscribe to this event), you are out of luck with ObservableCollection<T> because this collection only fires the CollectionChangedEvent.
Indeed, if you implement INotifyPropertyChanged, you will see changes to the items in the view (WPF does this automatically), but if you need to execute manual actions when an item changes, you can use BindingList<T>.
For exactly this scenario I rolled out a custom BindableCollection<T>, which implements ObservableCollection<T> and adds the OnItemChangedEvent. I can provide some sample code if necessary...
The collection doesn't know when the record is modified. To get a notification when this happens, the record needs to implement INotifyPropertyChanged

Where is DataGrid RowDeleted event?

Hello I am looking for an event I can handle for row deletion.
Instead of using an event on the DataGrid you should use a collection as its ItemsSource which supports change notification. Unfortunately the de-facto standard "ObservableCollection" does not support an event that fires before an item is removed, but it's quite easy to derive your own collection class from ObservableCollection which overrides RemoveItem and raises an appropriate event that can be cancelled.

Resources