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.
Related
I have two DataGrids bound to the same ObservableCollection property in my ViewModel.
When I edit an existing row in one DataGrid another gets updated after row is commited.
But how it works? The NotifyCollectionChanged event is not raised on the underlying ObservableCollection. I have checked it by subscribing to the event.
In this case, you are modifying the properties of the item (entity) of the collection, not the collection itself.
Therefore, the CollectionChanged event is not raised.
To update properties in an entity, INotifyPropertyChanged must be implemented in it.
But if the change of properties happens ONLY through bindings, then even this is not necessary.
Bindings use PropertyDescriptor and other types of reflection in their work.
This allows bindings to "know" that a property has been changed by another binding.
If you speak Russian, read my article: https://www.cyberforum.ru/wpf-silverlight/thread2650880.html
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.
I've got a simple View with a single textbox that gets databound to a simple ViewModel with a single string property.
I need to catch the TextChanged event of that textbox so that I can do a little validation magic.
The problem that I am running into is that the TextChanged event fires for that textbox when the DataContext is set for the View.
Is there a standard mechanism that I can use to determine if the event is firing because of the DataContext being set versus when the user is making changes?
Thanks!
As far as I know there is no such mechanism. What you should do instead is to do your validation magic using standard means of WPF. Please see the following link: http://msdn.microsoft.com/en-us/library/ms752347.aspx#data_validation.
Anyway, as long as you use MVVM you can always detect that text has changed in the setter of the bound property in your view model.
I use ComboBox.ItemsSource=[some data collection] to bind data to the control.
I want to hookup an event handler to the combobox so that whenever its data updated (or first time bound), I can do somthing.
The problem is I can't find an appropriate event for it. The close guess is DataContextChanged. But that is not invoked when the items get bound/created.
Many thanks in advance for any helps.
Cheers~
The ComboBox.Items property is of type ItemCollection, which has CollectionChanged, CurrentChanged, CurrentChanging events. They should suit your needs.
ItemCollection Class MSDN Article
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