How to get specific check/s on view model or model?

Dani_S 3,316 Reputation points
2023-05-19T06:39:28.7933333+00:00

Hi,

in continue to https://learn.microsoft.com/en-us/answers/questions/1286004/how-to-bind-select-all-specific-check-to-view-mode?comment=answer-1254197&page=1#comment-1282871

I would like to ask how to update the selected item when IsChecked state changes ?

is checked property in model while selected item in view model.

Thanks,

.NET MAUI
.NET MAUI
A Microsoft open-source framework for building native device applications spanning mobile, tablet, and desktop.
3,220 questions
{count} votes

Accepted answer
  1. Leon Lu (Shanghai Wicresoft Co,.Ltd.) 72,011 Reputation points Microsoft Vendor
    2023-05-22T07:34:28.1266667+00:00

    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.

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful