Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using a WPF controls own properties in Command Binding

I have a ToggleButton. I'm using a command binding, and I want to pass the value of its IsChecked property as a parameter. How can I do this without naming the ToggleButton and using its name to address itself?

Currently I'm solving this by naming the control, but I assume this can be done a better way?

<ToggleButton x:Name="_myToggle" 
              Command="{Binding SomeCommand}" 
              CommandParameter="{Binding ElementName=_myToggle, Path=IsChecked}">
    Apply Toggle
</ToggleButton>
like image 607
stiank81 Avatar asked Feb 23 '10 09:02

stiank81


1 Answers

you need to use self binding :

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

Hope this helps!

like image 188
Natxo Avatar answered Oct 01 '22 21:10

Natxo