Navigator update after Remove

4 posts, 0 answers
  1. Kevin
    Kevin avatar
    26 posts
    Member since:
    Mar 2012

    Posted 02 Mar Link to this post

    I've got a RadCollectionNavigator with Source bound to an ObservableCollection<int> in the VM and CurrentItem two-way bound to an int in the VM. When I call ObservableCollection<int>.Remove I'm expecting the RadCollectionNavigator to update and change CurrentItem, but this isn't happening. Is this something it can do or do I have to implement an update to the CurrentItem binding myself in the VM?
  2. Yoan
    Admin
    Yoan avatar
    1070 posts

    Posted 03 Mar Link to this post

    Hello Kevin,

    I have tested this scenario on my end and it works as expected. Please check the attached sample project for a reference and let me know how it works for you.

    Regards,
    Yoan
    Telerik
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
  3. UI for WPF is Visual Studio 2017 Ready
  4. Kevin
    Kevin avatar
    26 posts
    Member since:
    Mar 2012

    Posted 07 Mar in reply to Yoan Link to this post

    Thanks, Yoan. But I think in your example, it's actually the ListBox that's updating SelectedItem after the Remove. If you comment out the ListBox element in the XAML:

    <!--<ListBox 
                SelectedItem="{Binding ElementName=collectionNavigator, Path=CurrentItem, Mode=TwoWay}"                  
                ItemsSource="{Binding ElementName=collectionNavigator, Path=CollectionView}" />-->

    you get the behavior I'm experiencing. Run the app, press <end> in the Navigator, then press <begin> in the Navigator. You should now be seeing Liverpool at the bottom of the window. Now click "Remove item". Nothing will change. But if you now click <end> then <begin> in the Navigator, you'll see "Manchester Utd." at the bottom of the window. So it doesn't seem like RadCollectionNavigator will update CurrentItem when an item is removed from the ObservableCollection.

  5. Yoan
    Admin
    Yoan avatar
    1070 posts

    Posted 10 Mar Link to this post

    Hello,

    Indeed, you are right, I have missed the part for updating the source collection from the ViewModel. However, you can easily overcome this by updating the Source of the CollectionNavigator manually:
    ObservableCollection<Club> sourceCollection;
            public MainWindow()
            {
                InitializeComponent();
     
                this.DataContext = new MyViewModel();
                sourceCollection = (this.DataContext as MyViewModel).Clubs;
                sourceCollection.CollectionChanged += Clubs_CollectionChanged;
     
                this.collectionNavigator.Source = sourceCollection;
     
            }
     
            void Clubs_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
            {
                this.collectionNavigator.Source = null;
                this.collectionNavigator.Source = sourceCollection;
            }
     
            private void Button1_Click(object sender, RoutedEventArgs e)
            {
            }
     
            private void Button2_Click(object sender, RoutedEventArgs e)
            {
                sourceCollection.RemoveAt(0);
            }

    I hope this helps.

    Regards,
    Yoan
    Telerik
    Do you need help with upgrading your AJAX, WPF or WinForms project? Check the Telerik API Analyzer and share your thoughts.
Back to Top