locked
Handling Button.Click on parent panel RRS feed

  • Question

  • Hi! How can I handle all Click events from any child button?

     

    <!-- How do I handle the Button.Click event in the Stackpanel ? -->
    <!-- I would like to do something like this  -->
    <StackPanel Button.Click="_commonBtnClickHandler" >
        <Button />
        <Button />
        <Button />
    </StackPanel>
       

    Larsi

    Thursday, October 22, 2009 5:40 AM

Answers

  • The problem is that the click event is marked as hanlded by the buttons. Another solution would be to have a custom button control , that inherits from button and overrides the onclick, marking the event as handled = false. In that case, you could this: 

    <StackPanel Button.Click="_commonBtnClickHandler" >
        <YourCustomButton />
        <YourCustomButton />
        <YourCustomButton />
    </StackPanel>
     
    Thursday, October 22, 2009 7:13 AM
  • I made a mistake: the StackPanel does not have the Click event defined. But you could simulate the click using the MouseLeftButtonDown and MouseLeftButtonUp events. The XAML would be like this:
     

    <StackPanel MouseLeftButtonDown="downhdlr" MouseLeftButtonUp="uphdlr" >
        <YourCustomButton />
        <YourCustomButton />
        <YourCustomButton />
    </StackPanel>
     
    Thursday, October 22, 2009 7:36 AM

All replies

  • I believe that each button has to have the event handler assigned in order for it to work.

    Thursday, October 22, 2009 5:47 AM
  • It is not possible. Though you can use the same (one) Event Handler for the Buttons.

     

            <StackPanel>
                <Button x:Name="btn1" Click="commonBtn_Click" />
                <Button x:Name="btn2" Click="commonBtn_Click" />
                <Button x:Name="btn3" Click="commonBtn_Click" />
            </StackPanel>
      
    Thursday, October 22, 2009 6:27 AM
  • The problem is that the click event is marked as hanlded by the buttons. Another solution would be to have a custom button control , that inherits from button and overrides the onclick, marking the event as handled = false. In that case, you could this: 

    <StackPanel Button.Click="_commonBtnClickHandler" >
        <YourCustomButton />
        <YourCustomButton />
        <YourCustomButton />
    </StackPanel>
     
    Thursday, October 22, 2009 7:13 AM
  • Hi Helena, thanks for suggesting this, but I get the error "The attachable property 'Click' was not found in type 'Button'. Should I create an attachable property (event?) on my custom control? Will it bubble?

    Thursday, October 22, 2009 7:19 AM
  • I made a mistake: the StackPanel does not have the Click event defined. But you could simulate the click using the MouseLeftButtonDown and MouseLeftButtonUp events. The XAML would be like this:
     

    <StackPanel MouseLeftButtonDown="downhdlr" MouseLeftButtonUp="uphdlr" >
        <YourCustomButton />
        <YourCustomButton />
        <YourCustomButton />
    </StackPanel>
     
    Thursday, October 22, 2009 7:36 AM
  • ok, thanks all.

    I've implemented it whith a behavior attached to each button. In the behavior I subscribe to the button click, and forwards it to a dummy control that acts as a bridge.

    Thursday, October 22, 2009 7:47 AM