Click here to Skip to main content
13,551,492 members
Rate this:
Please Sign up or sign in to vote.
See more:
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.

<CheckBox Content="" Height="16" HorizontalAlignment="Right" Margin="0,297,118,0" Name="checkBox1" VerticalAlignment="Top"

                  Command="{Binding CheckCommand, Mode=TwoWay}" CommandParameter="{Binding IsChecked}">

The VieModel:

private bool isChecked;
private ICommand checkCommand;
public bool IsChecked
            get { return isChecked; }
                isChecked = value;

public ICommand CheckCommand
                if (checkCommand == null)
                    checkCommand = new CommandBase(i => Checkprocess(i), null);
                return checkCommand; 
                checkCommand = value;
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?
Posted 18-Sep-12 3:01am

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

The code is almost completed. You can also bind properties. So change the XAML to:

<CheckBox Content="" Height="16" HorizontalAlignment="Right" Margin="0,297,118,0" Name="checkBox1" VerticalAlignment="Top"

                  Command="{Binding CheckCommand, Mode=TwoWay}" 

     IsChecked="{Binding Path=IsChecked, Mode=TwoWay}">

This will bind the IsChecked property of the CheckBox to the IsChecked property of your viewmodel. I have removed the CommandParameter in the Command. You won't need it because in your viewmodel you can use this.IsChecked to get the checked state of the CheckBox.
Bob-Brady 18-Sep-12 9:26am
That got it - thank you very much.

I knew it was going to be something obvious like that. I've only been doing MVVM and WPF for 3 weeks and I'm still learning.
Martijn-Kok 18-Sep-12 9:31am
WPF is just great. Most important is that one has to search for the obvious solution first, before making it more complicated.

I was wondering if you will need the Command at all, if you have the IsChecked. Actually I didn't know the CheckBox had a Command (still learning myself) If you only need to react to the CheckBox being set or not, the IsChecked binding could be sufficient. An advantage of the command might be the CanExecute functionality.
Clifford-Nelson 20-Sep-12 13:18pm
So why is Mode=TwoWay in Command="{Binding CheckCommand, Mode=TwoWay}"
Martijn-Kok 21-Sep-12 1:39am
Every binding has a default mode. The Modes are TwoWay, OneWay (from source (ViewModel) to target (View)), OneWayToSource (from target (View) to source (ViewModel), Default, Once (only once update on initialization). Binding to a TextBox's text property is TwoWay by default. Binding to a TextBlock's text property is OneWay by default.

I assume binding to a Command is TwoWay by default. At least I never explicitly define it. It was in your sample code. It worked, I guess I was lazy, so I left it there. :) You could remove it. In case of the IsChecked I defined it explicitly just to be sure, but probably IsChecked is also TwoWay by default.

Clifford-Nelson 21-Sep-12 2:57am
So explain how two way binding works with Command. Command is the ICommand interface, and provides a way to execute code. Not much point in TwoWay with ICommand. Incidently, a ToggleButton is basically a checkbox, which is probably why there is a Command argument.
Martijn-Kok 21-Sep-12 3:40am
Interesting question :) I'm not sure if two way binding is possible. It was in your code so I assumed it was possible. With a normal button it is not. At this moment there is no use I can think of why two way binding is possible.

As for the CheckBox it is kind of hybrid. It can both be used as something to display state (IsChecked) and to be used as a button (cuase it's derived from ButtonBase).

I'll try to study Command in the CheckBox after work. I have never used the Command of the CheckBox so I wonder how it works. Maybe the Command doesn't support TwoWay at all. And if it does I do wonder what it's use will be.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web02-2016 | 2.8.180515.1 | Last Updated 18 Sep 2012
Copyright © CodeProject, 1999-2018
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100