ComboBox强制崩溃(不是我的代码)

我从下面的客户报告了堆栈跟踪。 我不知道如何重现这一点。 我的WPF应用程序有相当数量的ComboBoxes; 鉴于下面的堆栈跟踪,我不确定如何确定哪个ComboBox失败。 有没有人见过这个? 你能解释一下这个堆栈跟踪的内容吗? 有任何想法吗?

System.NullReferenceException: Object reference not set to an instance of an object. at System.Windows.Controls.ComboBox.CoerceIsSelectionBoxHighlighted(Object o, Object value) at System.Windows.DependencyObject.ProcessCoerceValue(DependencyProperty dp, PropertyMetadata metadata, EntryIndex& entryIndex, Int32& targetIndex, EffectiveValueEntry& newEntry, EffectiveValueEntry& oldEntry, Object& oldValue, Object baseValue, Object controlValue, CoerceValueCallback coerceValueCallback, Boolean coerceWithDeferredReference, Boolean coerceWithCurrentValue, Boolean skipBaseValueChecks) at System.Windows.DependencyObject.UpdateEffectiveValue(EntryIndex entryIndex, DependencyProperty dp, PropertyMetadata metadata, EffectiveValueEntry oldEntry, EffectiveValueEntry& newEntry, Boolean coerceWithDeferredReference, Boolean coerceWithCurrentValue, OperationType operationType) at System.Windows.DependencyObject.CoerceValue(DependencyProperty dp) at System.Windows.Controls.ComboBox.OnIsKeyboardFocusWithinChanged(DependencyPropertyChangedEventArgs e) at System.Windows.FocusWithinProperty.FireNotifications(UIElement uie, ContentElement ce, UIElement3D uie3D, Boolean oldValue) at System.Windows.ReverseInheritProperty.FirePropertyChangeInAncestry(DependencyObject element, Boolean oldValue, DeferredElementTreeState treeState, Action`2 originChangedAction) at System.Windows.ReverseInheritProperty.OnOriginValueChanged(DependencyObject oldOrigin, DependencyObject newOrigin, IList`1 otherOrigins, DeferredElementTreeState& oldTreeState, Action`2 originChangedAction) at System.Windows.Input.KeyboardDevice.ChangeFocus(DependencyObject focus, Int32 timestamp) at System.Windows.Input.KeyboardDevice.PostProcessInput(Object sender, ProcessInputEventArgs e) at System.Windows.Input.InputManager.RaiseProcessInputEventHandlers(ProcessInputEventHandler postProcessInput, ProcessInputEventArgs processInputEventArgs) at System.Windows.Input.InputManager.ProcessStagingArea() at System.Windows.Input.InputManager.ProcessInput(InputEventArgs input) at System.Windows.Input.InputProviderSite.ReportInput(InputReport inputReport) at System.Windows.Interop.HwndKeyboardInputProvider.ReportInput(IntPtr hwnd, InputMode mode, Int32 timestamp, RawKeyboardActions actions, Int32 scanCode, Boolean isExtendedKey, Boolean isSystemKey, Int32 virtualKey) at System.Windows.Interop.HwndKeyboardInputProvider.PossiblyDeactivate(IntPtr hwndFocus) at System.Windows.Interop.HwndKeyboardInputProvider.FilterMessage(IntPtr hwnd, WindowMessage message, 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 System.Windows.Threading.ExceptionWrapper.TryCatchWhen(Object source, Delegate callback, Object args, Int32 numArgs, Delegate catchHandler) 

这是代码(.NET 4.5.2)。 o不是ComboBoxHighlightedElement为null。

就个人而言,我的第一步是分发PDB,以便在堆栈跟踪中获取行号。

 private static object CoerceIsSelectionBoxHighlighted(object o, object value) { ComboBox comboBox = (ComboBox)o; return (!comboBox.IsDropDownOpen && comboBox.IsKeyboardFocusWithin) || (comboBox.HighlightedInfo != null && comboBox.HighlightedElement.Content == comboBox._clonedElement); } private ComboBoxItem HighlightedElement { get { return (_highlightedInfo == null) ? null : _highlightedInfo.Container as ComboBoxItem; } } 

我们有一个定制的过滤combobox,我们作为一个inheritance自combobox的用户控件制作。 在Windows 10计算机上,我们开始在数据网格行的表单中过滤combobox中出现此错误。 我们在DataGrid上的DataGrid.RowDetailsTemplate中有一个过滤的combobox。

为了使错误消失,我们在Filtered_Combobox类中覆盖了这个子。

 Protected Overrides Sub OnIsKeyboardFocusWithinChanged(e As DependencyPropertyChangedEventArgs) Try Catch ex As Exception End Try End Sub 

注意:我们还没有在覆盖中放置任何代码,因为它似乎没有做任何事情(尽管崩溃应用程序)。

我们在某些(没有时间达到精确范围)版本的运行时和窗口上遇到了类似的问题。

我们的一个combobox具有以下风格

   

它以某种方式,有时与选择突出显示混乱并导致exception。 也许这很有帮助。

最终为我们解决的问题是覆盖导致问题的事件:

 Protected Overrides Sub OnIsKeyboardFocusWithinChanged(e As DependencyPropertyChangedEventArgs) Try 'GW 2015-09-20 Added this override to prevent windows 10 crashes on comboboxes within forms within datagrids Catch ex As Exception End Try End Sub 

我得到了与maiksaray共享的类似代码相同的错误。 对我来说,CoerceIsSelectionBoxHighlighted的NullReferenceException只发生在Windows 10上,而不是我的Windows 7开发机器上。 它只发生在第一次单击combobox打开时。

在我的情况下,我在加载视图时以编程方式打开和关闭combobox:

 public MyView() { InitializeComponent(); Loaded += OnLoaded; } private void comboBox1_DropDownOpened(object sender, EventArgs e) { comboBox1.ItemsSource = MyClass.GetComboBoxList(); } private void OnLoaded(object sender, RoutedEventArgs e) { comboBox1.IsDropDownOpen = true; comboBox1.IsDropDownOpen = false; } 

我这样做是另一个问题的解决方法,如下所述: http : //blog.elgaard.com/2009/09/03/wpf-making-combo-box-items-disabled-also-when-accessed-using-该键盘/

DevicesComboBox_DropDownOpened完成后发生错误。 但是,它只发生在OnLoaded代码上。 如果我注释掉Loaded + = OnLoaded ,那么我没有得到错误。

我的解决方案是简单地避免以编程方式打开和关闭ComboBox。