Silverlight 4 Databound CheckBoxList and RadioButtonList Controls to ViewModel


The answer is to use the ItemsControl and the ItemsControl.ItemTemplate property. 

<ItemsControl x:Name="m_itemsControl" ItemsSource="{Binding}">
            <CheckBox Content="{Binding Text}" IsChecked="{Binding IsSelected, Mode=TwoWay}" />

Provide a ViewModel with a Collection of Child ViewModel Type that implements INotifyPropertyChanged that will be associated with each CheckBox/RadioButton. The Child ViewModel Type will look like this:

public class SelectableItem : INotifyPropertyChanged
    bool IsSelected { get; set; }
    string Text { get; set; }
    string Value { get; set; }       


// You can also use an interface for an extra layer of abstraction too.

This class provides the Text to display in the list and also a boolean value to display whether the item is selected or not. 

Click here to download the source code.



Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s