0
Answer

How to get items in ViewModel from Multiselect Listbox -MVVM

Math Ew

Math Ew

8y
540
1
Hi, 
I have 3 ListBoxes in my app.
ListBox_01
  1. ItemsSource="{Binding EmployeeViewM.MainActivity}"  
EmployeeViewM is a ViewModel defined in ViewModelLocator using Ioc.
MainActivity is an ObservableCollection bind to the Model - table with activities via EntityFramework.
- SelectionMode = "Multiple"
- Limit to selected items set to 2
  1. <i:Interaction.Behaviors>  
  2.    <listb:ListBoxSelectedItemBehavior SelectedItems="{Binding EmployeeViewM.SelectedAll, Mode=TwoWay}"/>  
  3.    <limit:LimitSelectionBehavior Limit="2"/>  
  4. </i:Interaction.Behaviors>  
 - listb:ListBoxSelectedItemBehavior is an attached property to enable SelectedItems from ListBox
  1. public static readonly DependencyProperty SelectedItemsProperty = DependencyProperty.Register("SelectedItems"typeof(ObservableCollection<object>), typeof(ListBoxSelectedItemBehavior), new PropertyMetadata(new ObservableCollection<object>(), PropertyChangedCallback));  
 - SelectedAll is a property on EmployeeViewM ViewModel for SelectedItems (IList):
 
  1. private IList _selectedAll = new ArrayList();  
  2. public IList SelectedAll {  
  3.     get { return _selectedAll; }  
  4.     set {  
  5.         _selectedAll = value;  
  6.         RaisePropertyChanged("SelectedAll");  
  7.     }  
  8. }  
ListBox_02 is the same like ListBox_01, the only difference is that it is bind to another table (NonProcess)

Employees are able to select items on both - max 2 items per ListBox.

All selected items will be visible in ListBox_03
  1. ItemsSource="{Binding EmployeeViewM.SelectedAll}"  
ItemsSource for this ListBox is SelectedAll.

Scenario:
Employee select activity from ListBox_01. This should execute RelayCommand from ViewModel via SelectionChange to get which activity has been chosen by an employee, get attributes for this activity (attribute (Name, Group, Process etc) comes from Entity to which ListBox is bind via ItemsSource) save activity (attributes) to the database/entity, get ID from entity, and run DispatcherTimer to calculate time of activity, this activity should be visible on ListBox_03, if an employee will select another activity from ListBox_01 or Listbox_02, the scenario is similar but it should stop previous activity and get all attributes for newly selected activity. I'm not able to get those items in ViewModel and my SelectedAll has no columns, and I'm not able to bind IsSelected to it,
hope someone will be able to help me with this case...
 
Thank you for any suggestion