我得到了客户报告的下面的堆栈跟踪。我不知道如何重现这个。我的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)
答案 0 :(得分:6)
这是代码(.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; }
}
答案 1 :(得分:2)
我们有一个定制的过滤组合框,我们作为继承自组合框的用户控制而制作。在Windows 10计算机上,我们开始在数据网格行上的表单中过滤组合框中出现此错误。我们在DataGrid上的DataGrid.RowDetailsTemplate中有一个过滤的组合框。
为了让错误消失,我们在Filtered_Combobox类中覆盖了这个子。
Protected Overrides Sub OnIsKeyboardFocusWithinChanged(e As DependencyPropertyChangedEventArgs)
Try
Catch ex As Exception
End Try
End Sub
注意:我们还没有在覆盖中放置任何代码,因为它似乎没有做任何事情(尽管崩溃了应用程序)。
答案 2 :(得分:2)
我们在某些(没有时间到达精确范围)版本的运行时和窗口上遇到了类似的问题。
我们的一个组合框具有以下风格
<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>
不知何故,有时会因选择突出显示而导致异常。 也许这很有帮助。
答案 3 :(得分:1)
最终为我们解决的问题是覆盖导致问题的事件:
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
答案 4 :(得分:0)
我和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 代码中。如果我注释掉已加载+ = OnLoaded ,那么我没有收到错误。
我的解决方案是简单地避免以编程方式打开和关闭ComboBox。