D
D
Daniil Demidko2015-11-12 07:34:06
.NET
Daniil Demidko, 2015-11-12 07:34:06

How can I make it so that when a button is clicked, a click event is generated only for it?

8d2b8c74df3c48689b406a6f1cf6c3aa.JPG
There is a Xaml file with markup - a button, the button's content property, in turn, has a picture (which is not important) and TogleButton - that's what it is about.
How can I make sure that when a TogleButton is clicked, the click event is generated ONLY for the TogleButton, and not for the entire button?
If anything, I'm writing a file manager.
Screen is attached.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Sergey Mozhaykin, 2015-11-12
@Daniro_San

Set the RoutedEventArgs.Handled = true in the ToggleButton handler:

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApplication1"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525">
    <Button Click="Button_Click">
        <ToggleButton Click="ToggleButton_Click" />
    </Button>
</Window>

private void Button_Click(object sender, RoutedEventArgs e)
{
    MessageBox.Show("Button clicked");
}

private void ToggleButton_Click(object sender, RoutedEventArgs e)
{
    MessageBox.Show("ToggleButton clicked");
    e.Handled = true;
}

A
Alexey Pavlov, 2015-11-12
@lexxpavlov

You can try splitting the buttons. Grid makes it very easy to display one element on top of another. Or, according to your business logic, ToggleButton must be inside the Button?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question