开发者

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

开发者 https://www.devze.com 2023-02-25 11:24 出处:网络
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.

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 con开发者_开发百科nection 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.

0

精彩评论

暂无评论...
验证码 换一张
取 消