gpt4 book ai didi

WPF DataGrid,添加行时应用程序崩溃

转载 作者:行者123 更新时间:2023-12-04 03:04:45 26 4
gpt4 key购买 nike

我有一个绑定(bind)到 TrackableCollection 的 wpf 数据网格。在极少数情况下,并且仅针对少数选定用户,当用户通过输入底部空白行添加新记录时,应用程序将崩溃。
我无法重现这个问题,我所拥有的只是抛出异常的堆栈跟踪。
有没有人见过这样的事情?我对自动化对等类的了解有限,但我可以确认我们没有在我们的应用程序中使用它们中的任何一个。

这是堆栈跟踪:

System.ArgumentNullException: Value cannot be null.
Parameter name: item
at System.Windows.Automation.Peers.DataGridAutomationPeer.CreateItemAutomationPeer(Object item)
at System.Windows.Automation.Peers.ItemsControlAutomationPeer.FindOrCreateItemAutomationPeer(Object item)
at System.Windows.Automation.Peers.DataGridAutomationPeer.RaiseAutomationSelectionEvents(SelectionChangedEventArgs e)
at System.Windows.Controls.DataGrid.OnSelectionChanged(SelectionChangedEventArgs e)
at System.Windows.Controls.Primitives.Selector.SelectionChanger.End()
at System.Windows.Controls.DataGrid.MakeFullRowSelection(Object dataItem, Boolean allowsExtendSelect, Boolean allowsMinimalSelect)
at System.Windows.Controls.DataGrid.HandleSelectionForCellInput(DataGridCell cell, Boolean startDragging, Boolean allowsExtendSelect, Boolean allowsMinimalSelect)
at System.Windows.Controls.DataGridCell.OnAnyMouseLeftButtonDown(MouseButtonEventArgs e)
at System.Windows.RoutedEventArgs.InvokeHandler(Delegate handler, Object target)
at System.Windows.EventRoute.InvokeHandlersImpl(Object source, RoutedEventArgs args, Boolean reRaised)
at System.Windows.UIElement.ReRaiseEventAs(DependencyObject sender, RoutedEventArgs args, RoutedEvent newEvent)
at System.Windows.RoutedEventArgs.InvokeHandler(Delegate handler, Object target)
at System.Windows.EventRoute.InvokeHandlersImpl(Object source, RoutedEventArgs args, Boolean reRaised)
at System.Windows.UIElement.RaiseEventImpl(DependencyObject sender, RoutedEventArgs args)
at System.Windows.UIElement.RaiseTrustedEvent(RoutedEventArgs args)
at System.Windows.Input.InputManager.ProcessStagingArea()
at System.Windows.Input.InputProviderSite.ReportInput(InputReport inputReport)
at System.Windows.Interop.HwndMouseInputProvider.ReportInput(IntPtr hwnd, InputMode mode, Int32 timestamp, RawMouseActions actions, Int32 x, Int32 y, Int32 wheel)
at System.Windows.Interop.HwndMouseInputProvider.FilterMessage(IntPtr hwnd, WindowMessage msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
at System.Windows.Interop.HwndSource.InputFilterMessage(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)

XAML:
<DataGrid Name="OrdreSLinjeGrid" 
AutoGenerateColumns="False"
CanUserResizeRows="False"
CanUserAddRows="{Binding KanLeggeTilOrdreLinjer}"
HorizontalScrollBarVisibility="Disabled"
VerticalScrollBarVisibility="Visible"
ItemsSource="{Binding Order.OrderLines, Mode=TwoWay}" CanUserSortColumns="False"
SelectedItem="{Binding ValgtOrdreLinje}" >
<DataGrid.Columns>
<DataGridTextColumn
Header="{t:Translate Antall}"
TextAlignment="Right"
Width="50"
HeaderStyle="{StaticResource HøyrejustertColumnHeader}"
Binding="{Binding Antall, UpdateSourceTrigger=LostFocus}" />

<DataGridTextColumn
Header="{t:Translate Pris}"
Width="60"
HeaderStyle="{StaticResource HøyrejustertColumnHeader}"
Binding="{Binding Pris, UpdateSourceTrigger=LostFocus, ValidatesOnDataErrors=True}"
/>
</DataGrid.Columns>
</DataGrid>

任何输入或建议将不胜感激。

最佳答案

该问题与 DataGridAutomationPeer.RaiseAutomationSelectionEvents 中的错误有关。简单来说,内部方法不检查 SelectedItem 属性是否为空。它可以通过在 Windows 7 中运行“Microsoft Narrator”工具轻松复制。

因为这是一个密封类,所以除了使用 Reflection.Emit 或任何模拟工具拦截此方法外,没有简单的方法可以修复它,即使它已修复,我们也不能保证 Microsoft 不会更改此方法名称、签名或行为.

我通过从 DataGrid 继承实现了一个“足够好”的修复,这将改变当 SelectedItem 为空时取消选择 DataGrid 的方式,但前提是存在叙述者/触摸屏。

希望该错误将很快得到修复。如有必要,添加对 UIAutomationProvider 的引用。

using System.Windows.Automation.Peers;
using System.Windows.Controls;

public class TooDataGrid: DataGrid
{
protected override void OnSelectionChanged(SelectionChangedEventArgs e)
{
if(AutomationPeer.ListenerExists(AutomationEvents.SelectionItemPatternOnElementSelected) ||
AutomationPeer.ListenerExists(AutomationEvents.SelectionItemPatternOnElementAddedToSelection) ||
AutomationPeer.ListenerExists(AutomationEvents.SelectionItemPatternOnElementRemovedFromSelection))
{
if(SelectedItem == null)
{
return;
}
}
base.OnSelectionChanged(e);
}
}

关于WPF DataGrid,添加行时应用程序崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14352615/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com