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?

有帮助吗?

解决方案

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);
        }
    }
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top