Search code examples
c#xamlmvvm

I would like to know if there is a way to sync the checkboxes?


is there a way to sync the checkboxes?

for example my screen : enter image description here

I'm using the mvvm structure

XAML:

<CheckBox 
    Grid.Column="0"
    Grid.Row="0"
    Content="{lang:Localization Chk_Users}"
    IsEnabled="True"
    IsChecked="{Binding IsUserMigration}" Margin="20,20,20,20"
/>

 <CheckBox 
    Grid.Column="0"
    Grid.Row="1"
    Content="{lang:Localization Chk_Cards}"
    IsChecked="{Binding IsCardMigration}" Margin="20,6,18,34"
>

however I would like to know if there is, for example, if cards are selected, users are also selected automatically

The ViewModel only has the functions to do the migration... nothing related


Solution

  • Let say you have a ViewModel like this:

    public class MyViewModel
    {
        // Fields to be migrated
        public bool IsUserMigration
        {
            get => _isUserMigration;
            set
            {
                if (_isUserMigration!= value)
                {
                    _isUserMigration= value;
                    OnPropertyChanged(nameof(IsUserMigration));
                }
            }
        }
    
        public bool IsCardMigration
        {
            get => _isCardMigration;
            set
            {
                if (_isCardMigration!= value)
                {
                    _isCardMigration= value;
                    OnPropertyChanged(nameof(IsCardMigration));
                }
            }
        }
    }
    

    If you want to select IsUserMigration every time IsCardMigration is selected, simply set IsUserMigration to true in the setter or IsCardMigration.

    public bool IsCardMigration
    {
        get => _isCardMigration;
        set
        {
            if (_isCardMigration!= value)
            {
                _isCardMigration= value;
                OnPropertyChanged(nameof(IsCardMigration));
                IsUserMigration = true;
            }
        }
    }