Using a WPF controls own properties in Command Binding
Asked Answered
I

1

7

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>
Immedicable answered 23/2, 2010 at 9:25 Comment(0)
P
14

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!

Pee answered 23/2, 2010 at 9:46 Comment(1)
Of course! Thx! Have done this before, but I had completely forgotten about it for a while there..Immedicable

© 2022 - 2024 — McMap. All rights reserved.