Issues with SelectedItem

3 posts, 1 answers
  1. Gopinath
    Gopinath avatar
    3 posts
    Member since:
    May 2019

    Posted 12 Jul 2019 Link to this post

     

    I have a listbox defined in the xaml -

    <telerik:RadListBox x:Name="CartLineItems" ItemsSource="{Binding LineItems}" SelectedItem="{Binding SelectedArticle, Mode=TwoWay}"
    ItemTemplate="{StaticResource ConfectioneryDataTemplate}" VerticalAlignment="Stretch"
    HorizontalAlignment="Stretch" Background="LightGray">
    </telerik:RadListBox>

     

    I frequently need to clear and repopulate the ItemsSource to refresh the Listbox. (LineItems is an observable collection). When I clear the collection, the SelectedItem is also cleared (visually). When I repopulate and set the SelectedItem object with data, there is no item in the ListBox which is selected. Below is the code -

    private void RefreshLineItems()
    {
        // Store the selected article for later use.
        LineItem selectedLineItem = null;
        if (SelectedArticle != null)
            selectedLineItem = SelectedArticle;
     
        // Clear all the line items.
        LineItems.Clear();
        // Iterate through the cart to re-populate LineItems and reset the selected article.
        if (ActiveCart != null)
        {
            foreach (LineItem lineItem in ActiveCart.lineItems)
            {
                LineItems.Add(lineItem);
            }
            if (selectedLineItem != null)
                SelectedArticle = ActiveCart.lineItems.Find(x => x.articleID == selectedLineItem.articleID);
        }
    }

     

    How do I get the selected item to re-appear on the listbox after the ItemsSource is cleared?

    Thanks.

    Gopinath

  2. Answer
    Drago
    Admin
    Drago avatar
    10 posts

    Posted 16 Jul 2019 Link to this post

    Hi Gopinath,

    The way you can achieve that is by implementing the INotifyPropertyChanged interface and use the PropertyChanged event or have the telerik ViewModelBase class and use the OnPropertyChanged method.

    We have created a sample project for you using ViewModelBase approach.

    Please test to see if that helps and let me know.

    Regards,
    Dragan G
    Progress Telerik
    Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
  3. Gopinath
    Gopinath avatar
    3 posts
    Member since:
    May 2019

    Posted 17 Jul 2019 in reply to Drago Link to this post

    Hello Dragen,

    Thanks for the sample project. I was able to fix the problem. 

    Its crazy to assume that lot of things work straight forward, especially if you are switching over from frameworks like Angular, etc.

     

    Regards,

    Gopinath

Back to Top