Idealism, if you want to follow the MVVM approach, all of your elements in the ObservableCollection must be ViewModel.
These viewModels must expose 2 properties, such as string Description and bool IsSelected .
Now you need to provide your ListBox with a style to display a checkbox and a text block for each DataModel with data support.
The following XAML implements this style. Note. The DataContext Usercontrol must contain a ViewModel containing the ObservableCollection<YourClass> Items { get; set; } ObservableCollection<YourClass> Items { get; set; } ObservableCollection<YourClass> Items { get; set; } , where YourClass provides a string Description { get; set; } string Description { get; set; } string Description { get; set; } and bool IsSelected { get; set; } bool IsSelected { get; set; } bool IsSelected { get; set; } . Obviously, you'll want to throw INotifyPropertyChanged magic INotifyPropertyChanged .
<Grid> <Grid.Resources> <Style x:Key="CheckBoxListStyle" TargetType="ListBox"> <Style.Resources> <Style TargetType="ListBoxItem"> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="ListBoxItem"> <StackPanel Orientation="Horizontal"> <CheckBox x:Name="itemChoix" Margin="5,5,0,0" IsChecked="{Binding IsSelected, Mode=TwoWay}" IsEnabled="{Binding IsEnabled, Mode=TwoWay}" /> <TextBlock Margin="5,5,0,0" Text="{Binding Description, Mode=TwoWay}" /> </StackPanel> </ControlTemplate> </Setter.Value> </Setter> </Style> </Style.Resources> <Setter Property="ItemsPanel"> <Setter.Value> <ItemsPanelTemplate> <WrapPanel Orientation="Vertical" /> </ItemsPanelTemplate> </Setter.Value> </Setter> <Setter Property="BorderThickness" Value="0" /> </Style> </Grid.Resources> <Grid.ColumnDefinitions> <ColumnDefinition/> </Grid.ColumnDefinitions> <Grid.RowDefinitions> <RowDefinition Height="25"/> <RowDefinition Height="*"/> </Grid.RowDefinitions> <Border Grid.Row="1" Style="{StaticResource BoxBorder}" Grid.Column="1" VerticalAlignment="Stretch" HorizontalAlignment="Stretch"> <Grid> <ListBox ItemsSource="{Binding Path=Items}" SelectionMode="Multiple" Style="{StaticResource CheckBoxListStyle}"/> </Grid> </Border> </Grid>
source share