2010-02-23 95 views
7

我有一个ToggleButton。我正在使用命令绑定,并且我想将它的IsChecked属性的值作为参数传递。我怎样才能做到这一点,而不命名ToggleButton并使用它的名称来处理它自己?使用WPF在命令绑定中控制自己的属性

目前我正在通过命名控件来解决这个问题,但我认为这可以通过更好的方式来完成?

<ToggleButton x:Name="_myToggle" 
       Command="{Binding SomeCommand}" 
       CommandParameter="{Binding ElementName=_myToggle, Path=IsChecked}"> 
    Apply Toggle 
</ToggleButton> 

回答

14

你需要使用自我约束力:

<ToggleButton x:Name="_myToggle" 
       Command="{Binding SomeCommand}" 
       CommandParameter="{Binding RelativeSource={RelativeSource Self}, 
             Path=IsChecked}"> 
    Apply Toggle 
</ToggleButton> 

希望这有助于!

+0

当然!谢谢!之前做过这些,但我已经完全忘记了一段时间.. – stiank81 2010-02-23 09:53:12