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

Dani_S 3,316 Reputation points


in continue to

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.


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


    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;
                    SelectModel = null;

    Here is my tested layout. You can refer to it.

    <CollectionView ItemsSource="{Binding Items}" SelectionMode="Single" SelectedItem="{Binding SelectModel}" >
                            <CheckBox IsChecked="{Binding Checked}"></CheckBox>
                            <Label Text="{Binding Name}"></Label>

    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