1

In a WPF project I have the following scenario:

  • I'm using MahApps.Metro framework.
  • I have a button that navigates to some page.
  • The button has a ToggleSwitch control inside its content.

Whenever I click the ToggleSwitch, the parent Button click event is triggered. How to prevent this behaviour?

MahApps.Metro framework controls event priority

<Button x:Name="btn1" Height="40" BorderThickness="0" Width="auto" HorizontalAlignment="Stretch" Margin="0,0,0,0" Style="{DynamicResource SquareButtonStyle}" Click="Button_Click">
       <Grid Margin="0,0,0,0" HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
            <Grid.ColumnDefinitions>
                       <ColumnDefinition Width="50" />
                       <ColumnDefinition Width="100" />
                       <ColumnDefinition Width="*" />
                       <ColumnDefinition Width="Auto" />
            </Grid.ColumnDefinitions>

            <TextBlock VerticalAlignment="Center" Width="auto" Grid.Column="1">Services</TextBlock>
            <Controls:ToggleSwitch Grid.Column="3" x:Name="enabledSwitch4"
                                    VerticalAlignment="Center"
                                    Width="Auto"
                                    Style="{StaticResource MahApps.Metro.Styles.ToggleSwitch.Win10}"
                                    OnLabel="" OffLabel=""
                                    IsChecked="True" HorizontalAlignment="Center" />
        </Grid>
</Button>
4
  • Why do you need this ToggleSwitsch inside the Button content?
    – punker76
    Nov 2, 2016 at 15:04
  • The design of the project I'm working on requires this. basically, there is a navigation sidebar, and the user has the option to turn on/off some services from any page he is in now. Nov 2, 2016 at 15:21
  • So you want to enable/disable the button click action with the toggle button? Or what do you do at the click event of the button and the toggle switch?
    – punker76
    Nov 2, 2016 at 15:34
  • There is a ContentControl, each button in the sidebar changes the content of the ContentControl with a specific UserControl. The ToggleSwitch is used to turn on/off some background service. What I need is allowing to click the ToggleSwitsch to control the background service without executing the (parent) Button click event. When I click the ToggleSwitch now, the Button click event fires since its a parent. Nov 2, 2016 at 15:49

1 Answer 1

3
+50

You can handle/prevent this at the Button's click event.

private async void ButtonClick(object sender, RoutedEventArgs e)
{
    if (e.OriginalSource != sender)
    {
        // do nothing if clicked the ToggleSwitch
        e.Handled = true;
    }
    else
    {
        // handle the button click action
        await this.ShowMessageAsync(string.Empty, "Yeah, you clicked the real button!");
    }
}

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.