Setting the SelectedItem when the Listbox is Loaded

5 posts, 1 answers
  1. Roar
    Roar avatar
    9 posts
    Member since:
    Jul 2012

    Posted 14 Sep 2012 Link to this post

    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
  2. Answer
    George
    Admin
    George avatar
    1332 posts

    Posted 14 Sep 2012 Link to this post

    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.

  3. UI for WPF is Visual Studio 2017 Ready
  4. Roar
    Roar avatar
    9 posts
    Member since:
    Jul 2012

    Posted 17 Sep 2012 Link to this post

    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
  5. jackson
    jackson avatar
    1 posts
    Member since:
    Dec 2015

    Posted 06 Apr in reply to George Link to this post

    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!

  6. Nasko
    Admin
    Nasko avatar
    585 posts

    Posted 08 Apr Link to this post

    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.
Back to Top
UI for WPF is Visual Studio 2017 Ready