SelectedItemsSource
With the Q1 2015 release version of UI for WPF, the ability to manipulate the SelectedItems collection of RadListBox through the ViewModel is now available. This is now possible with the brand new ListBoxSelectedItemsBehavior behavior and its SelectedItemsSource attached property. The SelectedItemsSource is a collection that synchronizes itself with the SelectedItems collection of RadListBox – thus if an item is added, removed or the collection is replaced/cleared those actions will be executed on the SelectedItems collection as well.
Using the SelectedItemsSource
The following example will demonstrate how to bind the SelectedItemsSource property of the ListBoxSelectedItemsBehavior to a collection of custom objects. In order the synchronization between the SelectedItemSource and the SelectedItems of RadListBox control to be possible the bound collection should implement both the IEnumerable and the INotifyCollectionChanged interfaces.
Synchronization won’t be possible if SelectedItemsSource is bound to a collection that does not implement INotifyCollectionChanged.
First, you need to create a new business object named for example Item. Its structure is shown in the code-snippet below:
Business object Item
public class Item
{
public Item(string name)
{
this.Name = name;
}
public string Name { get; set; }
}Next thing you have to do is to create a new class named ViewModel that inherits the Telerik ViewModelBase abstract class – inside it initialize two collections with sample data. The first one will be for the ItemsSource of RadListBox while the second one will be the data source for the SelectedItemsSource property:
Creating the ViewModel
private ObservableCollection<Item> items;
private ObservableCollection<Item> selectedItemsSource;
public ViewModel()
{
this.Items = this.GetItems(100);
this.SelectedItemsSource = new ObservableCollection<Item>() { this.Items[0], this.Items[2], this.Items[4], this.Items[6], this.Items[7] };
}
public ObservableCollection<Item> SelectedItemsSource
{
get
{
return this.selectedItemsSource;
}
set
{
if (this.selectedItemsSource != value)
{
this.selectedItemsSource = value;
this.OnPropertyChanged(() => this.SelectedItemsSource);
}
}
}
public ObservableCollection<Item> Items
{
get
{
return this.items;
}
set
{
if (this.items != value)
{
this.items = value;
this.OnPropertyChanged(() => this.Items);
}
}
}
private ObservableCollection<Item> GetItems(int size)
{
var result = new ObservableCollection<Item>();
for (int i = 0; i < size; i++)
{
result.Add(new Item(string.Format("Item {0}", i)));
}
return result;
}The SelectedItemsSource and the ItemsSource should be bound to collections of the same type of items.
Next you should declare the ViewModel as DataContext in your XAML:
Set the ViewModel as DataContext
>
<UserControl.DataContext>
<local:ViewModel/>
</UserControl.DataContext>
Finally, all you need to do is to set both created collection of items to the ItemsSource and ListBoxSelectedItemsBehavior. SelectedItemsSource:
Set the ItemsSource and SelectedItemsSource
<telerik:RadListBox x:Name="radListBox" ItemsSource="{Binding Items}"
DisplayMemberPath="Name"
SelectionMode="Multiple"
telerik:ListBoxSelectedItemsBehavior.SelectedItemsSource="{Binding SelectedItemsSource}"/>
The final result is shown on the snapshot below:

Find a runnable project of the previous example in the WPF Samples GitHub repository.