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

Setting the SelectedItem when the Listbox is Loaded

4 Answers 259 Views
ListBox
This is a migrated thread and some comments may be shown as answers.
Roar
Top achievements
Rank 1
Roar asked on 14 Sep 2012, 07:34 AM
Hi,
When my ListBox is loaded for the first time, I want it to already have the SelectedItem highlighted, as if it is already clicked. How can I achieve that?

I use the MVVM Light toolkit, and I have a TwoWay binding on the SelectedItem to a property in my ViewModel.

<telerik:RadListBox  ItemsSource="{Binding AllNetworks}" SelectedItem="{Binding SelectedNetwork, Mode=TwoWay}">
...
...
...
</telerik:RadListBox>

I know that the SelectedNetwork property is set correctly when the ViewModel is initialized, but the ListBox does not highlight the selected item.

Regards,
Roar Bjørndal-Rasmussen

4 Answers, 1 is accepted

Sort by
0
Accepted
George
Telerik team
answered on 14 Sep 2012, 03:05 PM
Hello,

Please, double check whether the selected network equals one of the networks in the RadListBox.ItemsSource collection. I prepared a sample project that covers the given scenario and everything works fine. Please, refer to the attachment and let me know if I missed something.

Kind regards,
George
the Telerik team

Time to cast your vote for Telerik! Tell DevPro Connections and Windows IT Pro why Telerik is your choice. Telerik is nominated in a total of 25 categories.

0
Roar
Top achievements
Rank 1
answered on 17 Sep 2012, 06:15 AM
Hi,

Thanks, the solution was as you pointed out to find the selected network in my ItemsSource collection, and to assign that to the SelectedItem. It now works like a charm. :-)

Regards,
Roar Bjørndal-Rasmussen
0
jackson
Top achievements
Rank 1
answered on 06 Apr 2016, 07:29 PM

The example works if one item is selected. How to make it work for multiple items? I tried to change selectedCountry to List<Country> and ObservableCollection<Country>, neither worked.

Please help. Thanks!

0
Nasko
Telerik team
answered on 08 Apr 2016, 06:51 AM
Hi Jackson,

In order to achieve the desired the SelectedItemsSource attached property needs to be set with the collection of items you need to select. Please, check the following article from our help documentation that provides detailed information how to achieve the desired:
http://docs.telerik.com/devtools/wpf/controls/radlistbox/features/selecteditemssource

We have also modified the sample project with the described in the article approach.

Hope this helps.

Regards,
Nasko
Telerik
Do you need help with upgrading your AJAX, WPF or WinForms project? Check the Telerik API Analyzer and share your thoughts.
Tags
ListBox
Asked by
Roar
Top achievements
Rank 1
Answers by
George
Telerik team
Roar
Top achievements
Rank 1
jackson
Top achievements
Rank 1
Nasko
Telerik team
Share this question
or