I have the following observableCollection:
Main View Model:
MainViewModel : INotifyProperyChanged public MainViewModel() { Accessories = new ObservableCollection<string>(); Accessories.Add("Cast"); Accessories.Add("Neck Ring"); Accessories.Add("Shoulder Wedges"); Accessories.Add("Grip Rings"); Accessories.Add("Head Rest"); Accessories.Add("Knee Roll"); Accessories.Add("Elbow Sling"); Accessories.Add("Thermoplastic Mask"); Accessories.Add("Vacuum Lock Bags"); Accessories.Add("Breast Board"); } public ObservableCollection<string> selectedAccessories = new ObservableCollection<string>(); public ObservableCollection<string> SelectedAccessories { get => selectedAccessories; set { if (selectedAccessories == value) return; else { selectedAccessories = value; OnPropertyChanged(nameof(SelectedAccessories)); } } public event PropertyChangedEventHandler PropertyChanged; public void OnPropertyChanged([CallerMemberName] string propertyName = null) { PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); }
Then on the XAML Side:
<CollectionView ItemsSource="{Binding Accessories}" SelectionMode="Multiple" SelectedItems="{Binding SelectedAccessories}" /> <Label x:Name="label1" Text="{Binding SelectedAccessories}" /> <ListView x:Name="list1" ItemsSource="{Binding SelectedAccessories}" />
I would like to display in either a label or listview (both work). Now, Selected Accessories is an observable collection that would need to be the ItemsSource for the new list.
I also tried a different PropertyChanged method and it still doesn't work:
public event PropertyChangedEventHandler PropertyChanged; publicvoid OnPropertyChanged2(ObservableCollection<string> selectedAccessories) { PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(selectedAccessories.ToString())); }
The same structure work for other data types but I can't make it work for ObservableCollections. Do I need to create a method to retrieve the strings of the selected items? How can I access the selectedItems?
Answers
I couldn't see the whole code of yours, but after I tested page VerticalListMultiplePreSelectionPage.xaml of the sample code and added a listview to display the selected Items (
SelectedMonkeys
) , it works properly.The bind model is MonkeysViewModel.cs and the main code is:
The tested xaml is:
The result is:

Hi @matheuspacifici,have you resolved your question?