如何在相同的复选框命令中使用ischeck作为命令参数?

时间:2021-05-04 00:38:33

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.

因此,我有一个使用WPF/MVVM来触发命令的复选框,这很好,但是我想使用复选框的IsChecked属性作为命令参数。我试着这一点。

  <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!

谢谢!

4 个解决方案

#1


28  

Please add RelativeSource Self in CommandParameter

请在CommandParameter中添加RelativeSource Self。

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

#2


3  

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

如果您遇到以下异常,如我所做的…

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

System.Windows.Data.Binding设置属性。RelativeSource抛出一个异常

Try this instead:

试试这个:

CommandParameter="{Binding RelativeSource={RelativeSource Self}, Path=IsChecked}"

#3


2  

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

是的JW1是正确的。你也可以像这样使用elemen名字,这样也可以

"{Binding Path=IsChecked,ElementName=chkAll}"

#4


1  

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.

与在复选框上创建命令不同,您可以使用CLR属性绑定IsChecked,并在CLR属性的setter上执行命令逻辑。这是另一个处理命令行为的方法。

#1


28  

Please add RelativeSource Self in CommandParameter

请在CommandParameter中添加RelativeSource Self。

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

#2


3  

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

如果您遇到以下异常,如我所做的…

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

System.Windows.Data.Binding设置属性。RelativeSource抛出一个异常

Try this instead:

试试这个:

CommandParameter="{Binding RelativeSource={RelativeSource Self}, Path=IsChecked}"

#3


2  

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

是的JW1是正确的。你也可以像这样使用elemen名字,这样也可以

"{Binding Path=IsChecked,ElementName=chkAll}"

#4


1  

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.

与在复选框上创建命令不同,您可以使用CLR属性绑定IsChecked,并在CLR属性的setter上执行命令逻辑。这是另一个处理命令行为的方法。