Binding SelectedItem to a property named "Item" sometimes causes NRE
- Dominant language
- C#
- Stars
- 7.7k
- Forks
- 1.3k
- Avg merge
- 1d 11h
- Merged PRs (30d)
- 61
Description
* .NET Core Version: (3.1.300)
* Windows version: (10.0.18363)
* Does the bug reproduce also in WPF for .NET Framework 4.8?: Yes
**Problem description:**
The problem is described fully on the following SO post:
https://stackoverflow.com/questions/17733338/combobox-selecteditem-databinding-nullreference-exception
The issue is with the behaviour of DetermineWhetherDBNullIsValid in PropertyPathWorker.
https://github.com/dotnet/wpf/blob/5841f8b2984301b6fe8a8153c30e8c13cad2eb16/src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/Data/PropertyPathWorker.cs#L1708-L1722
It is hard to diagnose because it occurs once the System.Data.dll is loaded.
**Actual behavior:**
Full repo and details are included in the SO post.
I have seen the same method throwing in a production app (albeit a ListBox.SelectedItem bound to property named "Item").
```
System.NullReferenceException: Object reference not set to an instance of an object.
at MS.Internal.Data.PropertyPathWorker.DetermineWhetherDBNullIsValid(Object item)
at MS.Internal.Data.PropertyPathWorker.DetermineWhetherDBNullIsValid()
at MS.Internal.Data.PropertyPathWorker.get_IsDBNullValidForUpdate()
at System.Windows.Data.BindingExpression.ConvertProposedValue(Object value)
at System.Windows.Data.BindingExpressionBase.UpdateValue()
at System.Windows.Data.BindingExpressionBase.ProcessDirty()
at System.Windows.Data.BindingExpressionBase.Dirty()
at System.Windows.Data.BindingExpressionBase.SetValue(DependencyObject d, DependencyProperty dp, Object value)
at System.Windows.DependencyObject.SetValueCommon(DependencyProperty dp, Object value, PropertyMetadata metadata, Boolean coerceWithDeferredReference, Boolean coerceWithCurrentValue, OperationType operationType, Boolean isInternal)
at System.Windows.DependencyObject.SetCurrentValueInternal(DependencyProperty dp, Object value)
at System.Windows.Controls.Primitives.Selector.UpdatePublicSelectionProperties()
at System.Windows.Controls.Primitives.Selector.SelectionChanger.End()
at System.Windows.Controls.Primitives.Selector.OnItemsChanged(NotifyCollectionChangedEventArgs e)
at System.Collections.Specialized.NotifyCollectionChangedEventHandler.Invoke(Object sender, NotifyCollectionChangedEventArgs e)
at System.Windows.Data.CollectionView.OnCollectionChanged(NotifyCollectionChangedEventArgs args)
at System.Windows.WeakEventManager.ListenerList`1.DeliverEvent(Object sender, EventArgs e, Type managerType)
at System.Windows.WeakEventManager.DeliverEvent(Object sender, EventArgs args)
at System.Windows.Data.CollectionView.OnCollectionChanged(NotifyCollectionChangedEventArgs args)
at System.Windows.Data.ListCollectionView.RefreshOverride()
at System.Windows.Data.CollectionView.RefreshInternal()
at System.Windows.Data.CollectionView.EndDefer()
at System.Windows.Data.CollectionView.DeferHelper.Dispose()
```
**Expected behavior:**
It should be possible to bind SelectedItem to an object named "Item" without hitting this issue.
**Minimal repro:**
See SO post.
Contributor guide
Assessment
This issue has not been assessed yet.