Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <ListBox x:Name="ListBox1" SelectionMode="Multiple" Margin="10,10,357,20" ItemsSource="{Binding Items}">
- <ListBox.ItemTemplate>
- <DataTemplate>
- <StackPanel Orientation="Horizontal">
- <CheckBox Content="{Binding}" IsChecked="False"/>
- </StackPanel>
- </DataTemplate>
- </ListBox.ItemTemplate>
- </ListBox>
- private void Button1_Click(object sender, RoutedEventArgs e)
- {
- listOfNumbers = new List<int>();
- listOfNumbers.Add(1);
- listOfNumbers.Add(5);
- listOfNumbers.Add(34);
- ListBox1.ItemsSource = listOfNumbers;
- }
- class IntWithChoiceVM : DependencyObject
- {
- public int Value
- {
- get { return (int)GetValue(ValueProperty); }
- set { SetValue(ValueProperty, value); }
- }
- public static readonly DependencyProperty ValueProperty =
- DependencyProperty.Register(
- "Value", typeof(int), typeof(IntWithChoiceVM));
- public bool IsSelected
- {
- get { return (bool)GetValue(IsSelectedProperty); }
- set { SetValue(IsSelectedProperty, value); }
- }
- public static readonly DependencyProperty IsSelectedProperty =
- DependencyProperty.Register(
- "IsSelected", typeof(bool), typeof(IntWithChoiceVM));
- }
- class ListChoiceVM : DependencyObject
- {
- public ListChoiceVM(List<int> initial)
- {
- Values = new ObservableCollection();
- foreach (var n in initial)
- Values.Add(new IntWithChoiceVM() { Value = n, IsSelected = true });
- }
- public ObservableCollection<IntWithChoiceVM> Values { get; private set; }
- public IEnumerable<int> GetSelectedInts()
- {
- return Values.Where(v => v.IsSelected).Select(v => v.Value);
- }
- }
- <ListBox SelectionMode="Multiple" ItemsSource="{Binding Values}">
- <ListBox.ItemTemplate>
- <DataTemplate>
- <CheckBox Content="{Binding Value}"
- IsChecked="{Binding IsSelected}"/>
- </DataTemplate>
- </ListBox.ItemTemplate>
- </ListBox>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement