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!
Please add RelativeSource Self in CommandParameter
<CheckBox Margin="3" Content="Clear Selected OEM"
Command="{Binding Path=ClearOemCommand}"
CommandParameter="{Binding Path=IsChecked, RelativeSource={RelativeSource Self}}" />
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}"
Yes JW1 is correct. You can use elemen name too like this, that would also work
"{Binding Path=IsChecked,ElementName=chkAll}"
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With