使用WPF控制命令绑定中的自己的属性

sti*_*k81 7 .net wpf commandbinding

我有一个ToggleButton.我正在使用命令绑定,我想将其IsChecked属性的值作为参数传递.如何在不命名ToggleButton并使用其名称来解决自身问题的情况下如何做到这一点?

目前我通过命名控件来解决这个问题,但我认为这可以做得更好吗?

<ToggleButton x:Name="_myToggle" 
              Command="{Binding SomeCommand}" 
              CommandParameter="{Binding ElementName=_myToggle, Path=IsChecked}">
    Apply Toggle
</ToggleButton>
Run Code Online (Sandbox Code Playgroud)

Nat*_*txo 14

你需要使用自我绑定:

<ToggleButton x:Name="_myToggle" 
              Command="{Binding SomeCommand}" 
              CommandParameter="{Binding RelativeSource={RelativeSource Self},
                                         Path=IsChecked}">
    Apply Toggle
</ToggleButton>
Run Code Online (Sandbox Code Playgroud)

希望这可以帮助!