Click here to Skip to main content
15,867,453 members
Please Sign up or sign in to vote.
5.00/5 (1 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.

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?
Posted

1 solution

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

XML
<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}">
</CheckBox>


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.
 
Share this answer
 
Comments
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.

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



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