Advertisement
Guest User

Untitled

a guest
Jun 18th, 2019
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.03 KB | None | 0 0
  1. <ListBox x:Name="ListBox1" SelectionMode="Multiple" Margin="10,10,357,20" ItemsSource="{Binding Items}">
  2. <ListBox.ItemTemplate>
  3. <DataTemplate>
  4. <StackPanel Orientation="Horizontal">
  5. <CheckBox Content="{Binding}" IsChecked="False"/>
  6. </StackPanel>
  7. </DataTemplate>
  8. </ListBox.ItemTemplate>
  9. </ListBox>
  10.  
  11. private void Button1_Click(object sender, RoutedEventArgs e)
  12. {
  13. listOfNumbers = new List<int>();
  14. listOfNumbers.Add(1);
  15. listOfNumbers.Add(5);
  16. listOfNumbers.Add(34);
  17.  
  18. ListBox1.ItemsSource = listOfNumbers;
  19. }
  20.  
  21. class IntWithChoiceVM : DependencyObject
  22. {
  23. public int Value
  24. {
  25. get { return (int)GetValue(ValueProperty); }
  26. set { SetValue(ValueProperty, value); }
  27. }
  28.  
  29. public static readonly DependencyProperty ValueProperty =
  30. DependencyProperty.Register(
  31. "Value", typeof(int), typeof(IntWithChoiceVM));
  32.  
  33. public bool IsSelected
  34. {
  35. get { return (bool)GetValue(IsSelectedProperty); }
  36. set { SetValue(IsSelectedProperty, value); }
  37. }
  38.  
  39. public static readonly DependencyProperty IsSelectedProperty =
  40. DependencyProperty.Register(
  41. "IsSelected", typeof(bool), typeof(IntWithChoiceVM));
  42. }
  43.  
  44. class ListChoiceVM : DependencyObject
  45. {
  46. public ListChoiceVM(List<int> initial)
  47. {
  48. Values = new ObservableCollection();
  49. foreach (var n in initial)
  50. Values.Add(new IntWithChoiceVM() { Value = n, IsSelected = true });
  51. }
  52.  
  53. public ObservableCollection<IntWithChoiceVM> Values { get; private set; }
  54.  
  55. public IEnumerable<int> GetSelectedInts()
  56. {
  57. return Values.Where(v => v.IsSelected).Select(v => v.Value);
  58. }
  59. }
  60.  
  61. <ListBox SelectionMode="Multiple" ItemsSource="{Binding Values}">
  62. <ListBox.ItemTemplate>
  63. <DataTemplate>
  64. <CheckBox Content="{Binding Value}"
  65. IsChecked="{Binding IsSelected}"/>
  66. </DataTemplate>
  67. </ListBox.ItemTemplate>
  68. </ListBox>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement