I have an application that essentially pushes 3 modal pages to display unique CollectionViews for user interaction. I went this route versus a Picker because of the data I'm attempting to display.
Anyway, I have my XAML set up, my ViewModel set up but I cannot for the life of me get the SelectionChangedCommand to work properly. I've tried 2 different methods and neither gives me the return that I'm looking for.
Base Code
XAML: WarehouseModalPage
<CollectionView
x:Name="warehouse_list"
Margin="5"
HorizontalOptions="FillAndExpand"
ItemSizingStrategy="MeasureFirstItem"
ItemsLayout="{x:Static ListItemsLayout.VerticalList}"
ItemsSource="{Binding Warehouses}" // This is my ObservableCollection pulled from an API Request
SelectedItems="{Binding SelectedWarehouses}"
SelectionChangedCommand="{Binding SelectionCollectionChanged}"
SelectionMode="Multiple">
<CollectionView.ItemTemplate>
<DataTemplate>
... ...
</DataTemplate>
</CollectionView.ItemTemplate>
</CollectionView>
1st Attempt - Use an ICommand to access SelectedItems which was bound to my object SelectedWarehouses
ViewModel: WarehouseModalViewModel
class WarehouseModalViewModel
{
public ObservableCollection<AuthWarehouses> Warehouses { get; set; }
public List<AuthWarehouses> SelectedWarehouses { get; set; }
public bool CanExecuteSelectionCommand(object obj)
public WarehouseModalViewModel()
{
Warehouses = new ObservableCollection<AuthWarehouses>();
LoadWarehouseCommand = new Command(async () => await UpdateValidWarehouseList());
LoadWarehouseCommand.Execute(null);
}
public ICommand SelectionCollectionChanged
{
get
{
return new Command(() =>
{
Debug.WriteLine("Add to SelectedItems" + SelectedWarehouses.Count);
Barrel.Current.Add("SelectedWarehouses", selected, TimeSpan.FromDays(1));
}
}
}
}
2nd Attempt - Use a variation of building the Command to access the data
ViewModel: WarehouseModalViewModel
class WarehouseModalViewModel
{
public ObservableCollection<AuthWarehouses> Warehouses { get; set; }
public List<AuthWarehouses> SelectedWarehouses { get; set; }
public bool CanExecuteSelectionCommand(object obj)
public bool CanExecuteSelectionCommand(object obj)
{
return this.SelectedWarehouses != null;
}
public void ExecuteSelectionCommand(object obj)
{
Debug.WriteLine("Added to Barrel... " + SelectedWarehouses.Count);
Barrel.Current.Add("SelectedWarehouses", SelectedWarehouses, TimeSpan.FromDays(1));
}
public WarehouseModalViewModel()
{
Warehouses = new ObservableCollection<AuthWarehouses>();
LoadWarehouseCommand = new Command(async () => await UpdateValidWarehouseList());
LoadWarehouseCommand.Execute(null);
}
public ICommand SelectionCollectionChanged
{
get
{
return new Command(ExecuteSelectionCommand, CanExecuteSelectionCommand);
}
}
}
No matter what route I take I either get:
1st Attempt: A Null error pointing to this line:
Debug.WriteLine("Add to SelectedItems" + SelectedWarehouses.Count);
2nd Attempt: My debug line never actually fires
Can anyone see where I may be going wrong with using the CollectionView and getting the list of selected items?