This is a migrated thread and some comments may be shown as answers.

SelectedItem behavior for IBindingList

2 Answers 113 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Scott Waye
Top achievements
Rank 2
Iron
Veteran
Iron
Scott Waye asked on 04 Mar 2021, 06:30 PM

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; }
    }

2 Answers, 1 is accepted

Sort by
0
Accepted
Vladimir Stoyanov
Telerik team
answered on 09 Mar 2021, 11:24 AM

Hello Scott,

Thank you for the shared code snippet. 

I investigated the described scenario and the reason for the described behavior is the implementation of the BindingListCollectionView's AddNew method (on line 597), which changes the current item to the newly added one. After that the SelectedItem of the RadGridView is synchronized with the current one due to the fact that the IsSynchronizedWithCurrentItem property is True. If you don't want this to be the case, you can set it to False:

<telerik:RadGridView IsSynchronizedWithCurrentItem="False" />

If you want to keep the synchronization, you can reset the CurrentItem in the RowEditEnded event (since it seems that the BindingListCollectionView does not have such logic). Here is what I have in mind:

private void RadGridView_RowEditEnded(object sender, GridViewRowEditEndedEventArgs e)
        {
            if(e.Row is GridViewNewRow && e.EditAction == GridViewEditAction.Cancel)
            {
                var gridView = sender as RadGridView;
                gridView.CurrentItem = null;
                gridView.CurrentItem = gridView.Items[0];
            }
        }

I hope you find this information helpful.

Regards,
Vladimir Stoyanov
Progress Telerik

Virtual Classroom, the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products quickly just got a fresh new look + new and improved content including a brand new Blazor course! Check it out at https://learn.telerik.com/.

0
Scott Waye
Top achievements
Rank 2
Iron
Veteran
Iron
answered on 10 Mar 2021, 05:08 PM
I see, thanks for the source code link.  I now realise this is out of your control.
Tags
GridView
Asked by
Scott Waye
Top achievements
Rank 2
Iron
Veteran
Iron
Answers by
Vladimir Stoyanov
Telerik team
Scott Waye
Top achievements
Rank 2
Iron
Veteran
Iron
Share this question
or