【问题标题】:NullReferenceException when binding a property to a button in Xceed Datagrid将属性绑定到 Xceed Datagrid 中的按钮时出现 NullReferenceException
【发布时间】:2011-06-18 21:29:38
【问题描述】:

这个让我发疯。该问题仅在数据网格大到需要滚动条时才会出现,因此我强烈建议数据虚拟化在其中发挥作用。从下面的 xaml 中可以看出,为此数据网格定义的最后一列是一个按钮,其可见性绑定到 DataGridCollectionView 上的布尔“ErrorsPresent”属性,该属性是控件的数据源。 出于某种原因,我不知道,当一个大数据集加载到控件中时,会有一些空行,每个属性都有空值。如果我注释掉最后一列,网格及其数据会正确显示,但是当实际处理行的数据时(如在转换器中),我会在 PresentationFramework 中得到这个讨厌的错误。很抱歉将其全部包含在内,但我想证明它的所有 Windows 代码:

'APreII.vshost.exe'(托管 (v4.0.30319)):已加载 'C:\Windows\Microsoft.Net\assembly\GAC_MSIL\Accessibility\v4.0_4.0.0.0__b03f5f7f11d50a3a\Accessibility.dll' System.Transactions 严重:0:http://msdn.microsoft.com/TraceCodes/System/ActivityTracing/2004/07/Reliability/Exception/UnhandledUnhandled exceptionAPreII.vshost.exeSystem.NullReferenceException,mscorlib,版本=4.0.0.0,文化=中性,PublicKeyToken=b77a5c561934e089对象引用未设置为对象的实例。在 System.Windows.Data.BindingExpression.TransferValue(对象 newValue,布尔 isASubPropertyChange) 在 System.Windows.Data.BindingExpression.Activate(对象项) 在 System.Windows.Data.BindingExpression.AttachToContext(AttachAttempt 尝试) 在 System.Windows.Data.BindingExpression.MS.Internal.Data.IDataBindEngineClient.AttachToContext(布尔最后机会) 在 MS.Internal.Data.DataBindEngine.Task.Run(Boolean lastChance) 在 MS.Internal.Data.DataBindEngine.Run(对象 arg) 在 MS.Internal.Data.DataBindEngine.OnLayoutUpdated(对象发送者,EventArgs e) 在 System.Windows.ContextLayoutManager.fireLayoutUpdateEvent() 在 System.Windows.ContextLayoutManager.UpdateLayout() 在 System.Windows.ContextLayoutManager.UpdateLayoutCallback(对象 arg) 在 System.Windows.Media.MediaContext.FireInvokeOnRenderCallbacks() 在 System.Windows.Media.MediaContext.RenderMessageHandlerCore(对象 resizedCompositionTarget) 在 System.Windows.Media.MediaContext.RenderMessageHandler(对象 resizedCompositionTarget) 在 System.Windows.Threading.ExceptionWrapper.InternalRealCall(委托回调,对象 args,Int32 numArgs) 在 MS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen(对象源,委托方法,对象 args,Int32 numArgs,委托 catchHandler) 在 System.Windows.Threading.Dispatcher.WrappedInvoke(委托回调,对象 args,Int32 numArgs,委托 catchHandler) 在 System.Windows.Threading.DispatcherOperation.InvokeImpl() 在 System.Threading.ExecutionContext.runTryCode(对象 userData) 在 System.Runtime.CompilerServices.RuntimeHelpers.ExecuteCodeWithGuaranteedCleanup(TryCode 代码,CleanupCode backoutCode,对象 userData) 在 System.Threading.ExecutionContext.Run(ExecutionContext executionContext,ContextCallback 回调,对象状态,布尔 ignoreSyncCtx) 在 System.Threading.ExecutionContext.Run(ExecutionContext executionContext,ContextCallback 回调,对象状态) 在 System.Windows.Threading.DispatcherOperation.Invoke() 在 System.Windows.Threading.Dispatcher.ProcessQueue() 在 System.Windows.Threading.Dispatcher.WndProcHook(IntPtr hwnd,Int32 msg,IntPtr wParam,IntPtr lParam,布尔值和处理) 在 MS.Win32.HwndWrapper.WndProc(IntPtr hwnd,Int32 msg,IntPtr wParam,IntPtr lParam,布尔值和处理) 在 MS.Win32.HwndSubclass.DispatcherCallbackOperation(对象 o) 在 System.Windows.Threading.ExceptionWrapper.InternalRealCall(委托回调,对象 args,Int32 numArgs) 在 MS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen(对象源,委托方法,对象 args,Int32 numArgs,委托 catchHandler) 在 System.Windows.Threading.Dispatcher.WrappedInvoke(委托回调,对象 args,Int32 numArgs,委托 catchHandler) 在 System.Windows.Threading.Dispatcher.InvokeImpl(DispatcherPriority 优先级,TimeSpan 超时,委托方法,对象 args,Int32 numArgs) 在 MS.Win32.HwndSubclass.SubclassWndProc(IntPtr hwnd,Int32 msg,IntPtr wParam,IntPtr lParam) 在 MS.Win32.UnsafeNativeMethods.DispatchMessage(MSG&msg) 在 System.Windows.Threading.Dispatcher.PushFrameImpl(DispatcherFrame 框架) 在 System.Windows.Application.RunInternal(窗口窗口) 在 System.Windows.Application.Run() 在 D:\tfs\Hermes\dev\Hermes5.0\src\UIServices\APreII\obj\Debug\App.g.cs:line 0 中的 APreII.App.Main() 在 System.AppDomain._nExecuteAssembly(RuntimeAssembly 程序集,字符串 [] 参数) 在 System.Runtime.Hosting.ApplicationActivator.CreateInstance(ActivationContext activationContext,字符串 [] activationCustomData) 在 Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssemblyDebugInZone() 在 System.Threading.ExecutionContext.Run(ExecutionContext executionContext,ContextCallback 回调,对象状态,布尔 ignoreSyncCtx) 在 System.Threading.ExecutionContext.Run(ExecutionContext executionContext,ContextCallback 回调,对象状态) 在 System.Threading.ThreadHelper.ThreadStart()System.NullReferenceException:对象引用未设置为对象的实例。 在 System.Windows.Data.BindingExpression.TransferValue(对象 newValue,布尔 isASubPropertyChange) 在 System.Windows.Data.BindingExpression.Activate(对象项) 在 System.Windows.Data.BindingExpression.AttachToContext(AttachAttempt 尝试) 在 System.Windows.Data.BindingExpression.MS.Internal.Data.IDataBindEngineClient.AttachToContext(布尔最后机会) 在 MS.Internal.Data.DataBindEngine.Task.Run(Boolean lastChance) 在 MS.Internal.Data.DataBindEngine.Run(对象 arg) 在 MS.Internal.Data.DataBindEngine.OnLayoutUpdated(对象发送者,EventArgs e) 在 System.Windows.ContextLayoutManager.fireLayoutUpdateEvent() 在 System.Windows.ContextLayoutManager.UpdateLayout() 在 System.Windows.ContextLayoutManager.UpdateLayoutCallback(对象 arg) 在 System.Windows.Media.MediaContext.FireInvokeOnRenderCallbacks() 在 System.Windows.Media.MediaContext.RenderMessageHandlerCore(对象 resizedCompositionTarget) 在 System.Windows.Media.MediaContext.RenderMessageHandler(对象 resizedCompositionTarget) 在 System.Windows.Threading.ExceptionWrapper.InternalRealCall(委托回调,对象 args,Int32 numArgs) 在 MS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen(对象源,委托方法,对象 args,Int32 numArgs,委托 catchHandler) 在 System.Windows.Threading.Dispatcher.WrappedInvoke(委托回调,对象 args,Int32 numArgs,委托 catchHandler) 在 System.Windows.Threading.DispatcherOperation.InvokeImpl() 在 System.Threading.ExecutionContext.runTryCode(对象 userData) 在 System.Runtime.CompilerServices.RuntimeHelpers.ExecuteCodeWithGuaranteedCleanup(TryCode 代码,CleanupCode backoutCode,对象 userData) 在 System.Threading.ExecutionContext.Run(ExecutionContext executionContext,ContextCallback 回调,对象状态,布尔 ignoreSyncCtx) 在 System.Threading.ExecutionContext.Run(ExecutionContext executionContext,ContextCallback 回调,对象状态) 在 System.Windows.Threading.DispatcherOperation.Invoke() 在 System.Windows.Threading.Dispatcher.ProcessQueue() 在 System.Windows.Threading.Dispatcher.WndProcHook(IntPtr hwnd,Int32 msg,IntPtr wParam,IntPtr lParam,布尔值和处理) 在 MS.Win32.HwndWrapper.WndProc(IntPtr hwnd,Int32 msg,IntPtr wParam,IntPtr lParam,布尔值和处理) 在 MS.Win32.HwndSubclass.DispatcherCallbackOperation(对象 o) 在 System.Windows.Threading.ExceptionWrapper.InternalRealCall(委托回调,对象 args,Int32 numArgs) 在 MS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen(对象源,委托方法,对象 args,Int32 numArgs,委托 catchHandler) 在 System.Windows.Threading.Dispatcher.WrappedInvoke(委托回调,对象 args,Int32 numArgs,委托 catchHandler) 在 System.Windows.Threading.Dispatcher.InvokeImpl(DispatcherPriority 优先级,TimeSpan 超时,委托方法,对象 args,Int32 numArgs) 在 MS.Win32.HwndSubclass.SubclassWndProc(IntPtr hwnd,Int32 msg,IntPtr wParam,IntPtr lParam) 在 MS.Win32.UnsafeNativeMethods.DispatchMessage(MSG&msg) 在 System.Windows.Threading.Dispatcher.PushFrameImpl(DispatcherFrame 框架) 在 System.Windows.Application.RunInternal(窗口窗口) 在 System.Windows.Application.Run() 在 D:\tfs\Hermes\dev\Hermes5.0\src\UIServices\APreII\obj\Debug\App.g.cs:line 0 中的 APreII.App.Main() 在 System.AppDomain._nExecuteAssembly(RuntimeAssembly 程序集,字符串 [] 参数) 在 System.Runtime.Hosting.ApplicationActivator.CreateInstance(ActivationContext activationContext,字符串 [] activationCustomData) 在 Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssemblyDebugInZone() 在 System.Threading.ExecutionContext.Run(ExecutionContext executionContext,ContextCallback 回调,对象状态,布尔 ignoreSyncCtx) 在 System.Threading.ExecutionContext.Run(ExecutionContext executionContext,ContextCallback 回调,对象状态) 在 System.Threading.ThreadHelper.ThreadStart()

Xaml。如您所见,我 TRIED 通过设置 CanContentScroll 和 ItemScrollingBehavior 属性来禁用虚拟化,但没有任何效果。 :

                <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>

只是为了咯咯笑,我是如何制作 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);
    }

感谢您的帮助。我只是不知道如何正确关闭数据虚拟化(如果这是问题的话),如何防止这些空记录(不在底层数据集和 DataGridCollectionView 中)被 CellContentTemplate Visibility 转换器处理。

【问题讨论】:

    标签: wpf xceed-datagrid


    【解决方案1】:

    我讨厌在 1 天后回答我自己的问题,但我们到了。叹。 尽管在我的转换器调用的 XAML 中,我得到了从一些 Xceed 文档中提取的祖先数据行,但仅将我关心的属性(只是“绑定”)发送到转换器似乎有效。 我的猜测是,最初的错误是由于在另一个线程上使用虚拟化完成了一些工作,因此缺少指向我的任何代码的堆栈跟踪。

    这行得通:

                            <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>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-02-03
      • 2018-07-15
      • 2018-07-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-04-30
      • 1970-01-01
      相关资源
      最近更新 更多