Answer the question
In order to leave comments, you need to log in
How can I make it so that when a button is clicked, a click event is generated only for it?
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
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;
}
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 questionAsk a Question
731 491 924 answers to any question