Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to use isChecked as a command parameter in that same check boxes command

Tags:

mvvm

wpf

So I have a check box that fires a command using WPF/MVVM this works fine but I want to use the IsChecked property of the check box as a command parameter. I tried this.

  <CheckBox Margin="3" Content="Clear Selected OEM" 
                              Command="{Binding Path=ClearOemCommand}" 
                              CommandParameter="{Binding Path=IsChecked}"/>

Bu I get an error in the output window that says

System.Windows.Data Error: 40 : BindingExpression path error: 'IsChecked' property not found on 'object'

I would know how to use find ancestor if I wanted to use the property from another control but I am stumped here - it's probably easier than I think... Just not making the connection in my mind.

Thanks!

like image 253
Kenn Avatar asked Apr 13 '11 13:04

Kenn


4 Answers

Please add RelativeSource Self in CommandParameter

  <CheckBox Margin="3" Content="Clear Selected OEM" 
   Command="{Binding Path=ClearOemCommand}" 
   CommandParameter="{Binding Path=IsChecked, RelativeSource={RelativeSource Self}}" />
like image 56
Arseny Avatar answered Nov 20 '22 01:11

Arseny


If you run into the following exception, as I did...

Set property System.Windows.Data.Binding.RelativeSource threw an exception

Try this instead:

CommandParameter="{Binding RelativeSource={RelativeSource Self}, Path=IsChecked}"
like image 31
jw1 Avatar answered Nov 20 '22 02:11

jw1


Yes JW1 is correct. You can use elemen name too like this, that would also work

"{Binding Path=IsChecked,ElementName=chkAll}"
like image 22
Kazi Avatar answered Nov 20 '22 02:11

Kazi


Instead of creating command on CheckBox you can bind IsChecked with a CLR property and perform your command logic on setter of CLR property. This is another workaround of handing of command behavior.

like image 1
pchajer Avatar answered Nov 20 '22 01:11

pchajer