Hello,
I've tried to build a usercontrol CheckListBox similar to
https://www.codeproject.com/Tips/1210187/%2FTips%2F1210187%2FA-WPF-CheckBox-ListBox
But this example doesn't show how to bind IsChecked or IsSelected to Viewmodels data.
In the xaml is the line IsChecked="{TemplateBinding IsSelected}"
I've build an observable list based on simple class "Satz" with IsChecked-property.
The app works, but my IsChecked-property Is'nt used, because I don't know how to connect it to "TemplateBinding IsSelected"
This is the esentiell Part of the xaml:
- <ListBox Margin="15"
- VerticalAlignment="Stretch"
- ItemsSource="{Binding Items}"
- SelectionMode="Multiple">
- <ListBox.Resources>
- <Style TargetType="ListBoxItem">
- <Setter Property="OverridesDefaultStyle" Value="true" />
- <Setter Property="SnapsToDevicePixels" Value="true" />
- <Setter Property="Template">
- <Setter.Value>
- <ControlTemplate TargetType="ListBoxItem">
- <CheckBox Margin="5,2"
- IsChecked="{TemplateBinding IsSelected}">
- <ContentPresenter />
- </CheckBox>
- </ControlTemplate>
- </Setter.Value>
- </Setter>
- </Style>
- </ListBox.Resources>
- </ListBox>
and this the part of ViewModel.cs
- public enum EnumCheckListBox
- {
- hare, hedgehog, deer, frog, fox, wolf, wild_boar
- }
- public class Satz
- {
- public EnumCheckListBox What { get; set; }
- public override string ToString()
- {
- return What.ToString();
- }
- public bool IsChecked { get; set; }
- }
- public class ViewModel
- {
- public ObservableCollection<Satz> Items { get; } = new ObservableCollection<Satz>();
- public ViewModel()
- {
- foreach (EnumCheckListBox enm in Enum.GetValues(typeof(EnumCheckListBox)))
- {
- Items.Add(new Satz {
- What = enm,
- IsChecked = enm == EnumCheckListBox.deer
- });
- }
- }
- }
best regards Dietmar