Question

I'm using a Telerik RadListBox (multi-select) in a Silverlight/C# application. First they wanted all items in the list to be selected by default. Ok, no problem:

RadListBox.SelectAllCommand.Execute(null, listboxname);

But now, one of the four items needs to be not selected by default, the other three selected. I've searched and searched for a code sample, fruitlessly. How can I accomplish this seemingly simple task?

Was it helpful?

Solution

Since the SelectedItems property of a RadListBox is of type IList, it is possible to still add items to that list instead of explicitly setting SelectedItems equal to another list.

For example, this will select all names not equal to 'Bobby' by default.

XAML:

<Grid>
    <telerik:RadListBox x:Name="ListBox"
                        SelectionMode="Multiple"/>
</Grid>

Code-Behind:

public partial class MainPage : UserControl
{
    public MainPage()
    {
        InitializeComponent();
        IList<string> names = new List<string>();
        names.Add("Alexander");
        names.Add("Bobby");
        names.Add("Chris");
        names.Add("Dean");

        ListBox.ItemsSource = names;

        foreach (var name in names.Where(x => x != "Bobby"))
        {
            ListBox.SelectedItems.Add(name);
        }
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top