问题 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)

3705
2018-03-27 17:24


起源

对于它的价值,我今天遇到了同样的问题,并设法解决了它。我在用 SetWidthFromItems 在我的组合框上。如果我清除列表并添加新项而不调用SetWidthFromItems,那么我会收到此错误。如果在添加元素后调用SetWidthFromItems,则不再发生错误。 - Anders Carstensen


答案:


这是代码(.NET 4.5.2)。或 o 不是 ComboBox 要么 HighlightedElement 一片空白。

就个人而言,我的第一步是分发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; }
}

6
2018-03-27 17:34



这是什么代码? - MyCodeSucks
@MyCodeSucks抛出异常的方法。看看他的筹码追踪。 - Zer0
也许他没有或没见过它?查看代码显示可能的空引用异常可能来自何处。 - Zer0
感谢您发布代码。这几乎看起来像WPF源代码中的拼写错误。他们应该检查一下 HighlightedElement为null而不是 HighlightedInfo。 - Brannon
连接问题表明修复将在4.6.2版本中。 connect.microsoft.com/VisualStudio/feedback/details/1660886/... - jbeanky


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

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

Protected Overrides Sub OnIsKeyboardFocusWithinChanged(e As DependencyPropertyChangedEventArgs)
    Try


    Catch ex As Exception
    End Try
End Sub

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


2
2017-08-20 14:45





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

我们的一个组合框具有以下风格

<ComboBox.Style>
    <Style TargetType="{x:Type ComboBox}" BasedOn="{StaticResource {x:Type ComboBox}}">
        <Setter Property="IsDropDownOpen" Value="False"/>
            <Style.Triggers>
                <Trigger Property="IsKeyboardFocusWithin" Value="True">
                    <Setter Property="IsDropDownOpen" Value="True" />
                </Trigger>
            </Style.Triggers>
    </Style>

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


2
2017-09-03 15:21





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

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

1
2017-11-26 11:06





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

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

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-the-keyboard/

之后发生了错误 DevicesComboBox_DropDownOpened 完成。然而,它只发生在 装载的 代码存在。如果我评论出来的话 加载+ = OnLoaded,然后我没有得到错误。

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


0
2018-02-04 15:42