Click here to Skip to main content
15,892,809 members

MVVM WPF4.0 Checkbox checked programatically

Bob Brady asked:

Open original thread
I've searched all over for this - while there is PLENTY on binding a checkbox, I couldn't find anything on programmatically setting the box as checked, and the solutions that I thought I'd found didn't work.

The XAML:
HTML
<CheckBox Content="" Height="16" HorizontalAlignment="Right" Margin="0,297,118,0" Name="checkBox1" VerticalAlignment="Top"
                  Command="{Binding CheckCommand, Mode=TwoWay}" CommandParameter="{Binding IsChecked}">
        </CheckBox>

The VieModel:

C#
private bool isChecked;
private ICommand checkCommand;
        
public bool IsChecked
        {
            get { return isChecked; }
            set
            {
                isChecked = value;
                OnPropertyChanged("IsChecked");
            }
        }

public ICommand CheckCommand
        {
            get 
            {
                if (checkCommand == null)
                    checkCommand = new CommandBase(i => Checkprocess(i), null);
                return checkCommand; 
            }
            set
            {
                checkCommand = value;
                OnPropertyChanged("CheckCommand");
            }
        }
public void Checkprocess(object sender)
        {
           //this DOES react when the checkbox is checked or unchecked
        }

public MyViwModel()
{
     IsChecked = true;
}

The value of IsChecked and isChecked DO become true, however the checkBox1 value remains unchecked.

What obvious thing am I missing here?
Tags: WPF, Checkbox, MVVM

Plain Text
ASM
ASP
ASP.NET
BASIC
BAT
C#
C++
COBOL
CoffeeScript
CSS
Dart
dbase
F#
FORTRAN
HTML
Java
Javascript
Kotlin
Lua
MIDL
MSIL
ObjectiveC
Pascal
PERL
PHP
PowerShell
Python
Razor
Ruby
Scala
Shell
SLN
SQL
Swift
T4
Terminal
TypeScript
VB
VBScript
XML
YAML

Preview



When answering a question please:
  1. Read the question carefully.
  2. Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  3. If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
  4. Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question.
Let's work to help developers, not make them feel stupid.
Please note that all posts will be submitted under the http://www.codeproject.com/info/cpol10.aspx.



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900