wpfcommandcustom-controls

WPF commands and custom controls


I'm making a custom control that's an expandable/collapsable panel, which I can then fill with other controls. I'm unsure of how to hook the expand/collapse command up to the button.

I've used this to get the panel to listen:

    CommandManager.RegisterClassInputBinding(GetType(Button), New InputBinding(ToggleBtnCommand, New MouseGesture(MouseAction.LeftClick)))
    CommandManager.RegisterClassCommandBinding(GetType(Button), New CommandBinding(ToggleBtnCommand, AddressOf btnToggleState))

This works, as long as I only use one panel per window. If I use more panels, all are expanded or collapsed when the button is pushed. How can I make the control understand that only the one containing the pushed button (the "command trigger") should listen to the command?


Solution

  • In your ToggleBtnCommand (it's really by the by but I'd personally remove Btn from the name as commands are UI agnostic) - execution handler you'd have ExecutedRoutedEventArgs as a secong argument, by comparing its Source & OriginalSource you can say whether it's a 'local' command or not.