在带有组合框和文本框的模板列中向上/向下滚动时应用程序崩溃 [英] Application crashes upon scrolling up/down in Template Column with ComboBoxes and TextBoxes

查看:33
本文介绍了在带有组合框和文本框的模板列中向上/向下滚动时应用程序崩溃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

正如标题所暗示的,滚动时应用程序崩溃.我添加了 MouseWheelPreviewMouseWheel 事件来处理列中的滚动,但有些仍然通过.

As the title suggests, the application crashes when scrolling. I added MouseWheel and PreviewMouseWheel events to handle the scrolling in the column, but some still make it through.

无法追溯到特定行,但我认为可能与以下代码有关:

A specific line cannot be traced back, but I believe it might be something to do with the following code:

TextBox ValueCell = (TextBox)sender;
if (GRID.SelectedItem == null || ValueCell == null || GRID.SelectedIndex <= 0) return;
//Obtain the new value
string Value = ValueCell.Text;   

//Obtain the item ID
DataGridRow Row = (DataGridRow)GRID.ItemContainerGenerator.ContainerFromIndex(GRID.SelectedIndex);
if (Row == null) return;
DataGridCell RowColumn = GRID.Columns[0].GetCellContent(Row).Parent as DataGridCell;

除了:

PresentationFramework.dll!System.Windows.Controls.DataGridRow.RestoreAttachedItemValue(System.Windows.DependencyObject objectWithProperty, System.Windows.DependencyProperty property) 加载了未知符号.

PresentationFramework.dll!System.Windows.Controls.DataGridRow.RestoreAttachedItemValue(System.Windows.DependencyObject objectWithProperty, System.Windows.DependencyProperty property) Unknown Symbols loaded.

调用栈是:

PresentationFramework.dll!System.Windows.Controls.DataGridRow.RestoreAttachedItemValue(System.Windows.DependencyObject objectWithProperty, System.Windows.DependencyProperty property)  Unknown Symbols loaded.
    PresentationFramework.dll!System.Windows.Controls.DataGridRow.SyncProperties(bool forcePrepareCells)    Unknown Symbols loaded.
    PresentationFramework.dll!System.Windows.Controls.DataGridRow.PrepareRow(object item, System.Windows.Controls.DataGrid owningDataGrid)  Unknown Symbols loaded.
    PresentationFramework.dll!System.Windows.Controls.DataGrid.PrepareContainerForItemOverride(System.Windows.DependencyObject element, object item)    Unknown Symbols loaded.
    PresentationFramework.dll!System.Windows.Controls.ItemsControl.MS.Internal.Controls.IGeneratorHost.PrepareItemContainer(System.Windows.DependencyObject container, object item) Unknown Symbols loaded.
    PresentationFramework.dll!System.Windows.Controls.ItemContainerGenerator.System.Windows.Controls.Primitives.IItemContainerGenerator.PrepareItemContainer(System.Windows.DependencyObject container) Unknown Symbols loaded.
    PresentationFramework.dll!System.Windows.Controls.VirtualizingStackPanel.InsertContainer(int childIndex, System.Windows.UIElement container, bool isRecycled)   Unknown Symbols loaded.
    PresentationFramework.dll!System.Windows.Controls.VirtualizingStackPanel.AddContainerFromGenerator(int childIndex, System.Windows.UIElement child, bool newlyRealized, bool isBeforeViewport)   Unknown Symbols loaded.
    PresentationFramework.dll!System.Windows.Controls.VirtualizingStackPanel.MeasureChild(ref System.Windows.Controls.Primitives.IItemContainerGenerator generator, ref System.Windows.Controls.Primitives.IContainItemStorage itemStorageProvider, ref System.Windows.Controls.Primitives.IContainItemStorage parentItemStorageProvider, ref object parentItem, ref bool hasUniformOrAverageContainerSizeBeenSet, ref double computedUniformOrAverageContainerSize, ref double computedUniformOrAverageContainerPixelSize, ref bool computedAreContainersUniformlySized, ref bool hasAnyContainerSpanChanged, ref System.Collections.IList items, ref object item, ref System.Collections.IList children, ref int childIndex, ref bool visualOrderChanged, ref bool isHorizontal, ref System.Windows.Size childConstraint, ref System.Windows.Rect viewport, ref System.Windows.Controls.VirtualizationCacheLength cacheSize, ref System.Windows.Controls.VirtualizationCacheLengthUnit cacheUnit, ref bool foundFirstItemInViewport, ref double firstItemInViewportOffset, ref System.Windows.Size stackPixelSize, ref System.Windows.Size stackPixelSizeInViewport, ref System.Windows.Size stackPixelSizeInCacheBeforeViewport, ref System.Windows.Size stackPixelSizeInCacheAfterViewport, ref System.Windows.Size stackLogicalSize, ref System.Windows.Size stackLogicalSizeInViewport, ref System.Windows.Size stackLogicalSizeInCacheBeforeViewport, ref System.Windows.Size stackLogicalSizeInCacheAfterViewport, ref bool mustDisableVirtualization, bool isBeforeFirstItem, bool isAfterFirstItem, bool isAfterLastItem, bool skipActualMeasure, bool skipGeneration, ref bool hasBringIntoViewContainerBeenMeasured, ref bool hasVirtualizingChildren)  Unknown Symbols loaded.
    PresentationFramework.dll!System.Windows.Controls.VirtualizingStackPanel.MeasureOverrideImpl(System.Windows.Size constraint, ref double? lastPageSafeOffset, ref System.Collections.Generic.List<double> previouslyMeasuredOffsets, ref double? lastPagePixelSize, bool remeasure)  Unknown Symbols loaded.
    PresentationFramework.dll!System.Windows.Controls.VirtualizingStackPanel.MeasureOverride(System.Windows.Size constraint)    Unknown Symbols loaded.
    PresentationFramework.dll!System.Windows.Controls.Primitives.DataGridRowsPresenter.MeasureOverride(System.Windows.Size constraint)  Unknown Symbols loaded.
    PresentationFramework.dll!System.Windows.FrameworkElement.MeasureCore(System.Windows.Size availableSize)    Unknown Symbols loaded.
    PresentationCore.dll!System.Windows.UIElement.Measure(System.Windows.Size availableSize)    Unknown Symbols loaded.
    PresentationCore.dll!System.Windows.ContextLayoutManager.UpdateLayout() Unknown Symbols loaded.
    PresentationCore.dll!System.Windows.ContextLayoutManager.UpdateLayoutCallback(object arg)   Unknown Symbols loaded.
    PresentationCore.dll!System.Windows.Media.MediaContext.InvokeOnRenderCallback.DoWork()  Unknown Symbols loaded.
    PresentationCore.dll!System.Windows.Media.MediaContext.FireInvokeOnRenderCallbacks()    Unknown Symbols loaded.
    PresentationCore.dll!System.Windows.Media.MediaContext.RenderMessageHandlerCore(object resizedCompositionTarget)    Unknown Symbols loaded.
    PresentationCore.dll!System.Windows.Media.MediaContext.RenderMessageHandler(object resizedCompositionTarget)    Unknown Symbols loaded.
    WindowsBase.dll!System.Windows.Threading.ExceptionWrapper.InternalRealCall(System.Delegate callback, object args, int numArgs)  Unknown Symbols loaded.
    WindowsBase.dll!System.Windows.Threading.ExceptionWrapper.TryCatchWhen(object source, System.Delegate callback, object args, int numArgs, System.Delegate catchHandler) Unknown Symbols loaded.
    WindowsBase.dll!System.Windows.Threading.DispatcherOperation.InvokeImpl()   Unknown Symbols loaded.
    WindowsBase.dll!System.Windows.Threading.DispatcherOperation.InvokeInSecurityContext(object state)  Unknown Symbols loaded.
    WindowsBase.dll!MS.Internal.CulturePreservingExecutionContext.CallbackWrapper(object obj)   Unknown Symbols loaded.
    mscorlib.dll!System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, object state, bool preserveSyncCtx) Line 954  C#  Symbols loaded.
    mscorlib.dll!System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, object state, bool preserveSyncCtx) Line 902  C#  Symbols loaded.
    mscorlib.dll!System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, object state) Line 891    C#  Symbols loaded.
    WindowsBase.dll!MS.Internal.CulturePreservingExecutionContext.Run(MS.Internal.CulturePreservingExecutionContext executionContext, System.Threading.ContextCallback callback, object state)  Unknown Symbols loaded.
    WindowsBase.dll!System.Windows.Threading.DispatcherOperation.Invoke()   Unknown Symbols loaded.
    WindowsBase.dll!System.Windows.Threading.Dispatcher.ProcessQueue()  Unknown Symbols loaded.
    WindowsBase.dll!System.Windows.Threading.Dispatcher.WndProcHook(System.IntPtr hwnd, int msg, System.IntPtr wParam, System.IntPtr lParam, ref bool handled)  Unknown Symbols loaded.
    WindowsBase.dll!MS.Win32.HwndWrapper.WndProc(System.IntPtr hwnd, int msg, System.IntPtr wParam, System.IntPtr lParam, ref bool handled) Unknown Symbols loaded.
    WindowsBase.dll!MS.Win32.HwndSubclass.DispatcherCallbackOperation(object o) Unknown Symbols loaded.
    WindowsBase.dll!System.Windows.Threading.ExceptionWrapper.InternalRealCall(System.Delegate callback, object args, int numArgs)  Unknown Symbols loaded.
    WindowsBase.dll!System.Windows.Threading.ExceptionWrapper.TryCatchWhen(object source, System.Delegate callback, object args, int numArgs, System.Delegate catchHandler) Unknown Symbols loaded.
    WindowsBase.dll!System.Windows.Threading.Dispatcher.LegacyInvokeImpl(System.Windows.Threading.DispatcherPriority priority, System.TimeSpan timeout, System.Delegate method, object args, int numArgs)   Unknown Symbols loaded.
    WindowsBase.dll!MS.Win32.HwndSubclass.SubclassWndProc(System.IntPtr hwnd, int msg, System.IntPtr wParam, System.IntPtr lParam)  Unknown Symbols loaded.
    [Native to Managed Transition]      Annotated Frame
    [Managed to Native Transition]      Annotated Frame
    WindowsBase.dll!System.Windows.Threading.Dispatcher.PushFrameImpl(System.Windows.Threading.DispatcherFrame frame)   Unknown Symbols loaded.
    WindowsBase.dll!System.Windows.Threading.Dispatcher.PushFrame(System.Windows.Threading.DispatcherFrame frame)   Unknown Symbols loaded.
    PresentationFramework.dll!System.Windows.Application.RunDispatcher(object ignore)   Unknown Symbols loaded.
    PresentationFramework.dll!System.Windows.Application.RunInternal(System.Windows.Window window)  Unknown Symbols loaded.
    PresentationFramework.dll!System.Windows.Application.Run(System.Windows.Window window)  Unknown Symbols loaded.
    PresentationFramework.dll!System.Windows.Application.Run()  Unknown Symbols loaded.
    App.Main()  Unknown Symbols loaded.

这里是异常抛出消息:抛出的异常:PresentationFramework.dll中的'System.NullReferenceException'类型为'System.NullReferenceException'的未处理异常发生在PresentationFramework.dll对象引用未设置为对象的实例.

上面的代码嵌套在一个 TextBox LostFocus 事件

The above code is nested within a TextBox LostFocus event

这是我的网格 XAML 代码:

Here is my XAML code for the grid:

 <DataGrid x:Name="GRID" SelectionMode="Single" CanUserAddRows="False" CanUserDeleteRows="False"
                  AutoGenerateColumns="False" Grid.Row="1" Grid.Column="1" Margin="5" AllowDrop="True" 
                  Drop="GRID_Drop" Grid.ColumnSpan="3" CellEditEnding="GRID_CellEditEnding" >
            <DataGrid.ItemContainerStyle>
                <Style TargetType="DataGridRow">
                    <Setter Property="HorizontalContentAlignment" Value="Stretch"/>


                </Style>
            </DataGrid.ItemContainerStyle>
            <DataGrid.Columns>
                <DataGridTextColumn x:Name="IdCol" Header="ID"  IsReadOnly="True"
                                    Binding="{Binding ItemId}" />
                <DataGridTextColumn x:Name="HexIdCol" Header="Hex ID"   IsReadOnly="True"
                                    Binding="{Binding HexId}" />
                <DataGridTextColumn x:Name="ItemCol" Header="Item" IsReadOnly="True"
                                    Binding="{Binding ItemNameFormatted}" />
                <DataGridComboBoxColumn x:Name="VCol" Header="V"
                                        SelectedItemBinding="{Binding Path=V, NotifyOnTargetUpdated=True}"
                                        DisplayMemberPath="{Binding Path=V}"/>


                <DataGridTemplateColumn x:Name="Va" Header="Va">
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <Grid MouseWheel="Grid_MouseWheel" PreviewMouseWheel="Grid_PreviewMouseWheel">
                                <TextBox    x:Name="VaTextBox" 
                                            Text="{Binding Data, Mode=TwoWay, NotifyOnSourceUpdated=True}"
                                            LostFocus="VaTextBox_LostFocus">
                                    <TextBox.Style>
                                        <Style TargetType="TextBox">
                                            <Setter Property="Visibility" Value="Collapsed"/>
                                            <Style.Triggers>
                                                <DataTrigger Binding="{Binding Options}" Value="{x:Null}">
                                                    <Setter Property="Visibility" Value="Visible"/>
                                                </DataTrigger>
                                            </Style.Triggers>
                                        </Style>
                                    </TextBox.Style>
                                </TextBox>
                                <ComboBox   x:Name="VaComboBox" ItemsSource="{Binding Options}" 
                                            SelectedItem="{Binding Data, Mode=TwoWay, NotifyOnSourceUpdated=True}" 
                                            SelectionChanged="VaComboBox_SelectionChanged">
                                    <ComboBox.Style>
                                        <Style TargetType="ComboBox">
                                            <Setter Property="Visibility" Value="Visible"/>
                                            <Style.Triggers>
                                                <DataTrigger Binding="{Binding Options}" Value="{x:Null}">
                                                    <Setter Property="Visibility" Value="Collapsed"/>
                                                </DataTrigger>
                                            </Style.Triggers>
                                        </Style>
                                    </ComboBox.Style>
                                </ComboBox>
                            </Grid>
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>

                <DataGridTextColumn x:Name="RaCol" Header="Ra"
                                    Binding="{Binding Ra, NotifyOnTargetUpdated=True}" />
            </DataGrid.Columns>
        </DataGrid>

推荐答案

问题已通过在 XAML 中添加 VirtualizingPanel.IsVirtualizing="False" 解决标签

Issue was solved by adding VirtualizingPanel.IsVirtualizing="False" in the <DataGrid> XAML tag

这篇关于在带有组合框和文本框的模板列中向上/向下滚动时应用程序崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆