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

Issues with SelectedItem

4 Answers 676 Views
ListBox
This is a migrated thread and some comments may be shown as answers.
Gopinath
Top achievements
Rank 1
Gopinath asked on 12 Jul 2019, 01:25 PM

 

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

4 Answers, 1 is accepted

Sort by
0
Accepted
Drago
Telerik team
answered on 16 Jul 2019, 01:29 PM
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.
0
Gopinath
Top achievements
Rank 1
answered on 17 Jul 2019, 09:58 AM

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

0
Sweidan
Top achievements
Rank 1
answered on 03 Apr 2021, 01:16 AM
Hi Drago!

I try to build the code example you provided, but I get the following error:

Could not resolve this reference. Could not locate the assembly "Telerik.Windows.Controls, Version=2019.2.618.40, Culture=neutral, PublicKeyToken=5803cfa389c90ce7, processorArchitecture=MSIL". Check to make sure the assembly exists on disk. If this reference is required by your code, you may get compilation errors.

Can I download the missing assembly, and add it to the project via NuGet package manager?
0
Vladimir Stoyanov
Telerik team
answered on 05 Apr 2021, 09:35 AM

Hello Sweidan,

I am attaching the sample project updated with the necessary dll added so you can test it on your end. 

To answer your question, you can also download the UI for WPF dlls via NuGet. You can read some more about that in the Installing UI for WPF from a NuGet package article. 

I hope you find this 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/.

Tags
ListBox
Asked by
Gopinath
Top achievements
Rank 1
Answers by
Drago
Telerik team
Gopinath
Top achievements
Rank 1
Sweidan
Top achievements
Rank 1
Vladimir Stoyanov
Telerik team
Share this question
or