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