I have simplified this problem as much as I can. I basically redefine the "null" value of combobox. Thus, if the selected item is deleted, it returns to "(null)". Unfortunately, the behavior of this is wrong, I delete delete, the ObservableCollection element is deleted, so the property binding is updated and returns the "(null)" element as expected. But the appearance of the combobox appears blank. However, the value that it is associated with is correct ... this problem can be reproduced using the code below.
To reproduce this problem, you select an item and delete it. Please note that at this moment the next line is called (when deleting the selected item). So this is a good place for a breakpoint.
if (m_Selected == null) { return Items[0]; //items 0 is ItemNull }
Also note that I fixed the fix by running a property update in DisplayMemberPath. This did not work.
MainWindow.xaml
<Window x:Class="WPFCodeDump.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" Height="350" Width="525"> <StackPanel> <ComboBox ItemsSource="{Binding Items}" SelectedItem="{Binding Selected, Mode=TwoWay}" DisplayMemberPath="Name"></ComboBox> <Button Click="ButtonBase_OnClick">Remove Selected</Button> </StackPanel> </Window>
MainWindowViewModel.cs
using System.Collections.ObjectModel; using System.ComponentModel; using System.Runtime.CompilerServices; using System.Windows.Input; namespace WPFCodeDump { public abstract class ViewModelBase : INotifyPropertyChanged { public event PropertyChangedEventHandler PropertyChanged; protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = "") { PropertyChangedEventHandler handler = PropertyChanged; if (handler != null) { handler(this, new PropertyChangedEventArgs(propertyName)); } } }
MainWindow.xaml.cs
using System.Windows; namespace WPFCodeDump {
Result:

Asheh source share