I’m converting a program from WinForms to WPF. There seems to be a lot of unnecessary syntax changes. But the one I’m having trouble with is saving the “checked” or “unchecked” status to Properties.Settings. In WinForms I used:
private void chkBackup_CheckedChanged(object sender, EventArgs e)
{
Properties.Settings.Default.Backup = chkBackup.Checked;
Properties.Settings.Default.Save();
}
There doesn’t seem to be an Event for “CheckedChanged” in WPF, So I’m trying:
private void chkBackup_Checked(object sender, RoutedEventArgs e)
{
Properties.Settings.Default.Backup = (chkBackup.IsChecked == true);
Properties.Settings.Default.Save();
}
private void chkBackup_Unchecked(object sender, RoutedEventArgs e)
{
Properties.Settings.Default.Backup = (chkBackup.IsChecked == false);
Properties.Settings.Default.Save();
}
I get no errors with this, but when I uncheck the checkBox, the settings aren’t changed. Please help. What am I doing wrong.
Thanks
You’re using a different expression each time. In the checked event you’re using
chkBackup.IsChecked == truewhich evaluates to true if the box is checked and false otherwise.In the unchecked event you’re using
chkBackup.IsChecked == falsewhich evaluates to true if the box isn’t checked and false otherwise.What you’re interested in is if the box is checked or not. The expression to use for this is
chkBackup.IsChecked == true. Your current solution will always savetrue.