Version 2020.3.1020.310
I know BindingList is old from WinForms but why does it behave differently to ObservableCollection<T> and is it correct?
Take this xaml and the code and run it. When a new row is added the SelectedItem changes before accepting the new row. Press escape twice to cancel the new row and the SelectedItem does not change, it is still set to the now canceled item. Change the ItemsSource Binding from `TradeBindingList` to `Trades` and repeat. You'll notice that SelectedItem is not changed when adding a new row. This latter behavior is much more preferable and is correct as it does not leave the SelectedItem incorrect. Why is this?
<telerik:RadGridView ItemsSource="{Binding TradeBindingList}" IsSynchronizedWithCurrentItem="True" CanUserInsertRows="True" NewRowPosition="Top" SelectedItem="{Binding SelectedTrade, Mode=TwoWay}"
AutoGenerateColumns="False">
<telerik:RadGridView.Columns>
<telerik:GridViewDataColumn DataMemberBinding="{Binding TradeNumber}"></telerik:GridViewDataColumn>
</telerik:RadGridView.Columns>
</telerik:RadGridView>
public partial class MainWindow : Window, INotifyPropertyChanged
{
object selectedTrade;
public MainWindow()
{
InitializeComponent();
Trades = new ObservableCollection<Trade>
{
new Trade(),
};
TradeBindingList = new BindingList<Trade>
{
new Trade(),
};
DataContext = this;
}
public ObservableCollection<Trade> Trades { get; set; }
public BindingList<Trade> TradeBindingList { get; set; }
public object SelectedTrade
{
get => selectedTrade;
set
{
selectedTrade = value;
Debug.WriteLine("Selected changed");
OnPropertyChanged();
}
}
public event PropertyChangedEventHandler PropertyChanged;
[NotifyPropertyChangedInvocator]
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
public class Trade : ViewModelBase
{
public Trade()
{
TradeNumber = "1";
}
public string TradeNumber { get; set; }
}