Below is the simple snippet of what I'm trying to achieve. Expectation:
- Having two buttons one above another.
- While single/double click on top button, top button should hide and bottom button should be visible.
Issue :
When I double click on the top button, top button hides, but it triggers MouseDoubleClick in bottom button. And I'm not sure how to avoid that.
<Grid x:Name="innerGrid" > <Button x:Name="second" Content="click" Width="100" Background="Red" MouseDoubleClick="Button_MouseDoubleClick" Click="second_Click" Height="50"/> <Button x:Name="first" Content="click" Width="200" Background="Green" MouseDown="Button_MouseDown" Height="50"/> </Grid> private void Button_MouseDown(object sender, MouseButtonEventArgs e) { (sender as FrameworkElement).Visibility = Visibility.Collapsed; } private void Button_MouseDoubleClick(object sender, MouseButtonEventArgs e) { (sender as Button).Background = Brushes.Yellow; } private void second_Click(object sender, RoutedEventArgs e) { second.Background = Brushes.Red; first.Visibility = Visibility.Visible; }
I have tried putting e.Handled as true in Preview and Bubble events but not able to get the expected behavior.
Please let me know how to achieve that.
Thanks.
Here is an approach that should fix your issues and work the way you want. However it is a bit of a hack, and you lose the pressed state of the buttons.
The overall idea is to use
PreviewMouseDownevents andDispatcherTimers to keep single and double-clicks separate for each button.The first button just needs to process a single-click and make sure not to propagate a double-click. Because it's timer waits for all clicks within 200ms, we can easily do that.
The second button is unique in that it needs to separately process a double-click and a single-click. We use a separate 200ms timer that will tally up the click count and then will process the proper code.
Here is the edited XAML:
Here is the code-behind: