Hello,
You can do this by adding a Checked property in your Model and adding SelectModel( <CollectionView SelectedItem="{Binding SelectModel}">
) property in your viewModel.
Both of them implement the INotifyPropertyChanged
interface like this document:interactive-mvvm
After that, When you add new Model to the List
or ObservableCollection
, you can add PropertyChanged
event to the Model like following code in ViewModel.
Model model= new Model() { Name = "Name" + i };
model.PropertyChanged += Model_PropertyChanged;
Model_PropertyChanged
method will be executed when CheckBox is checked or not, we bind SelectModel
property to control the Collectionview's Itemselect,then we can set the value to SelectModel
to change the Collectionview's Itemselect.
we implement the Model_PropertyChanged
method like following code.
private void Model_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
{
Model model= sender as Model;
if (model.Checked)
{
SelectModel = model;
}
else
{
SelectModel = null;
}
}
Here is my tested layout. You can refer to it.
<CollectionView ItemsSource="{Binding Items}" SelectionMode="Single" SelectedItem="{Binding SelectModel}" >
<CollectionView.ItemTemplate>
<DataTemplate>
<StackLayout>
<CheckBox IsChecked="{Binding Checked}"></CheckBox>
<Label Text="{Binding Name}"></Label>
</StackLayout>
</DataTemplate>
</CollectionView.ItemTemplate>
</CollectionView>
Best Regards,
Leon Lu
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.