Question

I want to save and retrieve a check box value from database in mvvm model , I am using this below code .

checkbox Xaml:

<CheckBox x:Name="CbxAccess" 
          Margin="380,50,0,180" 
          FontSize="14" 
          IsChecked="{Binding IsActive, Mode=TwoWay}" 
          Checked="cbxhasAccess_Checked_1" 
          Unchecked="cbxhasAccess_Checked_1"  
          HorizontalAlignment="Left" 
          Width="20">
</CheckBox>

checkbox Xaml.cs :

 private void cbxhasAccess_Checked_1(object sender, RoutedEventArgs e)
        {
            var rbtn = sender as CheckBox;           
            var settingsmodel = new SettingsModel();
            if (rbtn.IsFocused)
            {
                if ((bool)rbtn.IsChecked)
                {
                    settingsmodel.IsActive = true;                        
                }
                else
                {
                    settingsmodel.IsActive = false;  
                }
            }            
        }

model :

 private bool isActive;       
        public bool IsActive
        {
            get
            {
                return isActive;
            }
            set
            {

                isActive = value;
                RaisePropertyChanged("IsActive");
            }
        }      

viewmodel :

 SettingsModel st = new SettingsModel();
        var createconfigureBatchJobsXElement = new XElement("UpgradeAccessSettings");
        createconfigureBatchJobsXElement.Add(new XElement("IsActive", st.IsActive));
        root.Add(createconfigureBatchJobsXElement);

in above viewmodel i am trying to get the active status and save the xelement to database , currently i am unable get the status properly , though it is checked i am getting false . I want to retrive the status as well from database and show that in UI and do some other operations in the application based on the status(my intention is to save the value and do some operations in application based on true or false ).

please help me , thanks in adavnce

Was it helpful?

Solution

There are few problems with your code, namely:

  • you're creating way too many SettingsModel instances (should have one, living in view model bound to your view)
  • cbxhasAccess_Checked_1 are not needed as you're binding with TwoWay mode

To fix this, first and foremost you should expose settings model (or IsActive property) on your view model:

// view model
public SettingsModel Settings { get; private set; }
// view model constructor
Settings = new SettingsModel();

Then in your view your binding changes to:

<CheckBox ... IsChecked="{Binding Settings.IsActive, Mode=TwoWay}" />

Note that cbxhasAccess_Checked_1 method is not needed.

This however (exposing settings model) is not the best idiomatic way to resolve this problem with MVVM. Instead, you could keep SettingsModel private within view model and wrap around it's IsActive property:

public bool IsActive
{
    get { return settingsModel.IsActive; }
    set
    {
        if (settingsModel.IsActive != value)
        {
            settingsModel.IsActive
            RaisePropertyChanged("IsActive");
        }
    }
}

Either way, important point is to have only one instance of SettingsModel within view model.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top