NullReferenceException when binding a property to a button in Xceed Datagrid

2.5k views Asked by At

This one is driving me nuts. The issue only shows up when the datagrid is large enough to require scroll bars, so I strongly suggest that data virtualisation plays a hand in this. As you can see from the xaml below the last column defined for this datagrid is a button whose visibility is bound to the boolean "ErrorsPresent" property on the DataGridCollectionView that is the datasource for the control. For some reason, unknown to me, when a big dataset is loaded into the control, there are some empty rows, with null values in every property. If I comment out the last column, the grid and its data display properly, but when the row's data is actually processed (as in the converter) I get this nasty error in the PresentationFramework. I'm sorry to include it all, but I want to demonstrate that its all Windows code :

'APreII.vshost.exe' (Managed (v4.0.30319)): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_MSIL\Accessibility\v4.0_4.0.0.0__b03f5f7f11d50a3a\Accessibility.dll' System.Transactions Critical: 0 : http://msdn.microsoft.com/TraceCodes/System/ActivityTracing/2004/07/Reliability/Exception/UnhandledUnhandled exceptionAPreII.vshost.exeSystem.NullReferenceException, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089Object reference not set to an instance of an object. at System.Windows.Data.BindingExpression.TransferValue(Object newValue, Boolean isASubPropertyChange) at System.Windows.Data.BindingExpression.Activate(Object item) at System.Windows.Data.BindingExpression.AttachToContext(AttachAttempt attempt) at System.Windows.Data.BindingExpression.MS.Internal.Data.IDataBindEngineClient.AttachToContext(Boolean lastChance) at MS.Internal.Data.DataBindEngine.Task.Run(Boolean lastChance) at MS.Internal.Data.DataBindEngine.Run(Object arg) at MS.Internal.Data.DataBindEngine.OnLayoutUpdated(Object sender, EventArgs e) at System.Windows.ContextLayoutManager.fireLayoutUpdateEvent() at System.Windows.ContextLayoutManager.UpdateLayout() at System.Windows.ContextLayoutManager.UpdateLayoutCallback(Object arg) at System.Windows.Media.MediaContext.FireInvokeOnRenderCallbacks() at System.Windows.Media.MediaContext.RenderMessageHandlerCore(Object resizedCompositionTarget) at System.Windows.Media.MediaContext.RenderMessageHandler(Object resizedCompositionTarget) at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs) at MS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen(Object source, Delegate method, Object args, Int32 numArgs, Delegate catchHandler) at System.Windows.Threading.Dispatcher.WrappedInvoke(Delegate callback, Object args, Int32 numArgs, Delegate catchHandler) at System.Windows.Threading.DispatcherOperation.InvokeImpl() at System.Threading.ExecutionContext.runTryCode(Object userData) at System.Runtime.CompilerServices.RuntimeHelpers.ExecuteCodeWithGuaranteedCleanup(TryCode code, CleanupCode backoutCode, Object userData) at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx) at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state) at System.Windows.Threading.DispatcherOperation.Invoke() at System.Windows.Threading.Dispatcher.ProcessQueue() at System.Windows.Threading.Dispatcher.WndProcHook(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled) at MS.Win32.HwndWrapper.WndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled) at MS.Win32.HwndSubclass.DispatcherCallbackOperation(Object o) at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs) at MS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen(Object source, Delegate method, Object args, Int32 numArgs, Delegate catchHandler) at System.Windows.Threading.Dispatcher.WrappedInvoke(Delegate callback, Object args, Int32 numArgs, Delegate catchHandler) at System.Windows.Threading.Dispatcher.InvokeImpl(DispatcherPriority priority, TimeSpan timeout, Delegate method, Object args, Int32 numArgs) at MS.Win32.HwndSubclass.SubclassWndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam) at MS.Win32.UnsafeNativeMethods.DispatchMessage(MSG& msg) at System.Windows.Threading.Dispatcher.PushFrameImpl(DispatcherFrame frame) at System.Windows.Application.RunInternal(Window window) at System.Windows.Application.Run() at APreII.App.Main() in D:\tfs\Hermes\dev\Hermes5.0\src\UIServices\APreII\obj\Debug\App.g.cs:line 0 at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args) at System.Runtime.Hosting.ApplicationActivator.CreateInstance(ActivationContext activationContext, String[] activationCustomData) at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssemblyDebugInZone() at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx) at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state) at System.Threading.ThreadHelper.ThreadStart()System.NullReferenceException: Object reference not set to an instance of an object. at System.Windows.Data.BindingExpression.TransferValue(Object newValue, Boolean isASubPropertyChange) at System.Windows.Data.BindingExpression.Activate(Object item) at System.Windows.Data.BindingExpression.AttachToContext(AttachAttempt attempt) at System.Windows.Data.BindingExpression.MS.Internal.Data.IDataBindEngineClient.AttachToContext(Boolean lastChance) at MS.Internal.Data.DataBindEngine.Task.Run(Boolean lastChance) at MS.Internal.Data.DataBindEngine.Run(Object arg) at MS.Internal.Data.DataBindEngine.OnLayoutUpdated(Object sender, EventArgs e) at System.Windows.ContextLayoutManager.fireLayoutUpdateEvent() at System.Windows.ContextLayoutManager.UpdateLayout() at System.Windows.ContextLayoutManager.UpdateLayoutCallback(Object arg) at System.Windows.Media.MediaContext.FireInvokeOnRenderCallbacks() at System.Windows.Media.MediaContext.RenderMessageHandlerCore(Object resizedCompositionTarget) at System.Windows.Media.MediaContext.RenderMessageHandler(Object resizedCompositionTarget) at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs) at MS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen(Object source, Delegate method, Object args, Int32 numArgs, Delegate catchHandler) at System.Windows.Threading.Dispatcher.WrappedInvoke(Delegate callback, Object args, Int32 numArgs, Delegate catchHandler) at System.Windows.Threading.DispatcherOperation.InvokeImpl() at System.Threading.ExecutionContext.runTryCode(Object userData) at System.Runtime.CompilerServices.RuntimeHelpers.ExecuteCodeWithGuaranteedCleanup(TryCode code, CleanupCode backoutCode, Object userData) at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx) at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state) at System.Windows.Threading.DispatcherOperation.Invoke() at System.Windows.Threading.Dispatcher.ProcessQueue() at System.Windows.Threading.Dispatcher.WndProcHook(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled) at MS.Win32.HwndWrapper.WndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled) at MS.Win32.HwndSubclass.DispatcherCallbackOperation(Object o) at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs) at MS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen(Object source, Delegate method, Object args, Int32 numArgs, Delegate catchHandler) at System.Windows.Threading.Dispatcher.WrappedInvoke(Delegate callback, Object args, Int32 numArgs, Delegate catchHandler) at System.Windows.Threading.Dispatcher.InvokeImpl(DispatcherPriority priority, TimeSpan timeout, Delegate method, Object args, Int32 numArgs) at MS.Win32.HwndSubclass.SubclassWndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam) at MS.Win32.UnsafeNativeMethods.DispatchMessage(MSG& msg) at System.Windows.Threading.Dispatcher.PushFrameImpl(DispatcherFrame frame) at System.Windows.Application.RunInternal(Window window) at System.Windows.Application.Run() at APreII.App.Main() in D:\tfs\Hermes\dev\Hermes5.0\src\UIServices\APreII\obj\Debug\App.g.cs:line 0 at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args) at System.Runtime.Hosting.ApplicationActivator.CreateInstance(ActivationContext activationContext, String[] activationCustomData) at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssemblyDebugInZone() at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx) at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state) at System.Threading.ThreadHelper.ThreadStart()

The Xaml. As you can see I've TRIED to disable virtualisation by setting the CanContentScroll & ItemScrollingBehavior properties, which has no effect. :

                <XceedDG:DataGridControl Grid.Column="2" x:Name="EncounterDataGrid" ItemsSource="{Binding Path = EncounterDataGridCollectionView, UpdateSourceTrigger=PropertyChanged}" 
                                     Background="{x:Static Views:DataGridControlBackgroundBrushes.ElementalBlue}" CellErrorStyle="{StaticResource cell_error}" 
                                     FontSize="12" AutoCreateColumns="False" ItemScrollingBehavior="Immediate" ScrollViewer.CanContentScroll="False"
                                     EditTriggers="None" NavigationBehavior="RowOnly" CellEditorDisplayConditions="None" >

                <XceedDG:DataGridControl.View>
                    <Views:TableView ColumnStretchMode="All" ShowRowSelectorPane="False" ScrollViewer.CanContentScroll="False" 
                     ColumnStretchMinWidth="100"/>
                </XceedDG:DataGridControl.View>
                <!--Group Header formatting-->
                <XceedDG:DataGridControl.Resources>
                    <DataTemplate DataType="{x:Type XceedDG:Group}">
                        <TextBlock Foreground="DarkSlateBlue"
                        Padding="3,3,3,3" VerticalAlignment="Center"
                        FontSize="14" FontWeight="Black" MaxWidth="850" TextWrapping="Wrap"
                        Text="{Binding Path=., Converter={StaticResource GroupHeaderTextConverterType}}"/>
                    </DataTemplate>
                </XceedDG:DataGridControl.Resources>
                <!--Visible column definitions-->
                <XceedDG:DataGridControl.Columns>
                    <XceedDG:Column MinWidth="50"
                     FieldName="AccountNumber" 
                     Title="Account #"
                     IsMainColumn="True" 
                     ShowInColumnChooser="False"
                     TextWrapping="Wrap" 
                     ReadOnly="True" 
                     Visible="True"/>
                    <XceedDG:Column MinWidth="50"
                     FieldName="DOB" 
                     Title="Date of Birth"  
                     IsMainColumn="False"
                     ShowInColumnChooser="False" 
                     TextWrapping="Wrap"
                     ReadOnly="True"  
                     Visible="True"/>

                    <XceedDG:Column MinWidth="4"
                     FieldName="Processed"
                     Title="Processed"
                     IsMainColumn="False" 
                     ShowInColumnChooser="False" 
                     TextWrapping="Wrap"
                     ReadOnly="True" 
                     Visible="True"/>
                   <XceedDG:Column MinWidth="25" 
                     FieldName="ErrorsPresent"
                     Title="Errors"
                     IsMainColumn="False" 
                     ShowInColumnChooser="False" 
                     TextWrapping="Wrap"
                     ReadOnly="True" 
                     Visible="True" >
                    <XceedDG:Column.CellContentTemplate>
                        <DataTemplate>
                            <Button Command="{StaticResource ShowErrorDialogCommand}" CommandParameter="{Binding Path=DataContext, RelativeSource={RelativeSource AncestorType={x:Type XceedDG:DataRow}}}" Foreground="Red"
                            Content="View Errors" FontSize="11"
                            Visibility="{Binding RelativeSource={RelativeSource AncestorType={x:Type XceedDG:DataRow}}, Converter={StaticResource VisibilityConverterBooleanDataRow}, ConverterParameter=ErrorsPresent}"/>
                        </DataTemplate>
                    </XceedDG:Column.CellContentTemplate>

                    </XceedDG:Column>

                </XceedDG:DataGridControl.Columns>
            </XceedDG:DataGridControl>

And just for giggles, how I'm making the DataGridCollectionView :

        private void CreateDataGridCollectionView()
    {
        EncounterDataGridCollectionView = new DataGridCollectionView(Encounters);
        EncounterDataGridCollectionView.Filter = new Predicate<object>(IsIncludedPt);
        //Group by unit
        PropertyGroupDescription groupDescription = new PropertyGroupDescription("UnitID");
        EncounterDataGridCollectionView.GroupDescriptions.Add(groupDescription);
    }

Thanks for any help. I just don't know how to properly turn off data virtualisation (if that IS the problem), of how to keep these null records (which are not in the underlying dataset and DataGridCollectionView) from getting processed by the CellContentTemplate Visibility converter.

1

There are 1 answers

0
Bob On BEST ANSWER

I hate answering my own question 1 day later, but here we are. Sigh. Although in the XAML for my converter call I was getting the ancestor datarow, which I pulled from some Xceed docs, sending only the property I care about (just 'Binding') to the converter seems to work. My guess is that the original error was resulting from some work being done with virtualisation on another thread, hence the lack of a stacktrace that points to any of my code.

This works :

                        <XceedDG:Column MinWidth="25" 
                     FieldName="ErrorsPresent"
                     Title="Errors"
                     IsMainColumn="False" 
                     ShowInColumnChooser="False" 
                     TextWrapping="Wrap"
                     ReadOnly="True" 
                     Visible="True" >
                    <XceedDG:Column.CellContentTemplate>
                        <DataTemplate>
                            <Button Command="{StaticResource ShowErrorDialogCommand}" CommandParameter="{Binding Path=DataContext, RelativeSource={RelativeSource AncestorType={x:Type XceedDG:DataRow}}}" Foreground="Red"
                                    Content="View Errors" FontSize="11"
                                    Visibility="{Binding Converter={StaticResource VisibilityConverterBooleanDataRow}}" />
                            </DataTemplate>
                    </XceedDG:Column.CellContentTemplate>