I have a ListBox in my ProWindow which is configured for Extended selection so that the user can select multiple items. I also have a button which is supposed to remove the selected items from the list. I set up the binding for the ListBox as an ObservableCollection. If the Selection mode for the Listbox was Single I would set up a binding for the SelectedItem property of the Listbox. However, when the selection mode is set up for Extended selection I want to use the SelectedItems property, not the SelectedItem property. But I am not sure how to set up the binding in my ViewModel so I can get the IList from the SelectedItems property, which is a read only property. (I don't need to set the SelectedItems, I only need to get it)
I have read that there is a way to pass the SelectedItems property of the ListBox to a Command using a CommandParameters property. It says that the CommandParameter should be declared before the Command binding. The button xaml code is supposed to look something like this:
<ListBox x:Name="lbxAPNList" HorizontalAlignment="Left" Height="143" Margin="10,93,0,0" VerticalAlignment="Top" Width="136" SelectionMode="Extended" ItemsSource="{Binding APNsList}" SelectedItem="{Binding SelectedAPN}"/>
<Button x:Name="btnRemoveFromList" Content="Remove from List" CommandParameter="{Binding ElementName=lbxAPNList, Path=SelectedItems}" Command="{Binding Path=CmdRemoveFromList}"/>
However, I am not sure how to set the ViewModel ICommand declaration up so that I can use that command parameter. I tried the following but it doesn't work, since the code is never triggered:
private ObservableCollection<string> _apnsList = new ObservableCollection<string>();
public ObservableCollection<string> APNsList
{
get { return _apnsList; }
set
{
SetProperty(ref _apnsList, value, () => APNsList);
}
}
private string _selectedAPN;
public string SelectedAPN
{
get { return _selectedAPN; }
set
{
SetProperty(ref _selectedAPN, value, () => SelectedAPN);
}
}
// ListParcels is populated by another button and matches the listbox
public static SortedSet<string> ListParcels { get; set; }
public ICommand CmdRemoveFromList(IList<object> SelectedItems)
{
get
{
return new RelayCommand(() =>
{
Message = "";
// Iterate selected items in APN List
foreach (var item in SelectedItems)
{
// Get item as a string
string _APN = item.ToString();
// Remove the APN from the ListParcels SortedList
ListParcels.Remove(_APN);
}
//Clear the dialog APN list
APNsList.Clear();
// Iterate the APNs in the module ListParcels variable
foreach (string ListParcel in ListParcels)
{
// Add the APNs to the dialog list
APNsList.Add(ListParcel);
}
});
}
}
Is there a way to get the SelectedItems property of a ListBox for use by a button ICommand declaration that works with the Pro SDK?