Silverlight 5 – ListBox with Checkboxes

This is how you make a Listbox show checkboxes for selection in Silverlight 5.

<ListBox x:Name="TestList" SelectionMode="Multiple">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <CheckBox
               IsChecked="{Binding RelativeSource={RelativeSource AncestorType=ListBoxItem}, Path=IsSelected}"
               Content="{Binding}" />
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

And here’s a bit of sample code to set it up

public MainPage()
{
    InitializeComponent();
    SetupListBox();
}

public void SetupListBox()
{
    TestList.ItemsSource = new String[]
    {
        "Andrew", "Frasier", "Mellon", "Dale"
    };
}

About this entry