microsoft/XamlBehaviorsWpf

System.Reflection.TargetInvocationException: Checkbox event fires when used in datagrid w/o virtualiztion when scrolling

cflint987 opened this issue · 2 comments

When using this library for events on datagrids, it will automatically fire off event if the datagrid is virtualized. Setting VirtualizingPanel.IsVirtualizing to false will stop the behavior but you will lose too much performance.

Below is a sample project to reproduce the behavior.

Steps to reproduce:

  1. Click load employees
  2. Click the checkbox on ID 1
  3. Click the checkbox on ID 2
  4. Scroll all the way down
  5. Scroll all the way up

https://github.com/cflint987/WpfBug

I forgot to mention this will also happen with the SelectionChanged event when a combox/enum is used in the DataGrid.

https://stackoverflow.com/questions/51968260/datagrid-selectionchanged-event-firing-when-scrolling

The error is not occurring because of a bug in behavior. It's the combination of the use of the MessageBox and the virtualization that is occurring as UI elements are reused and state is sync'd with the underlying bound object. To fix the exception, use the dispacther to use the correct synchronization context to call the MessageBox:

        private void NotifyUserMethod(NotificationMessage message)
        {
            Dispatcher.CurrentDispatcher.BeginInvoke(new Action(() => MessageBox.Show(message.Notification)));            
        }

However, you still have the issue of the event firing because as the check boxes are reused, their state must be sync'd with the underlying bound object which will cause the events to fire. You are better off calling your command when the value of the actual data changes, and not trying to rely on a behavior to do it.

Also, the behavior is the exact same using the old .NET Framework version of the behaviors versus this one.