Home > Software engineering >  ShowDialog Allowing Event Triggers on MainWindow Underneath WPF VB.NET
ShowDialog Allowing Event Triggers on MainWindow Underneath WPF VB.NET

Time:12-10

I have a main window and a dialog window. The mainwindow has a path on it with a PreviewMouseLeftButtonUp event and a button. The button opens a 'ShowDialog' for my dialog window. This dialog window has a label on it with a PreviewMouseLeftButtonDown event that closes the dialog. The dialog opens directly over the path. When I click the label to close the dialog, the path PreviewMouseLeftButtonUp event also fires. How do I prevent this? I hope this makes sense and thanks for any help you can give me on this.

Here is my code for the MainWindow:

<Window x:Class="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:DialogTest"
    mc:Ignorable="d"
    Title="MainWindow" Height="695" Width="958" WindowStartupLocation="CenterScreen">
<Grid>
    <Button x:Name="button" Content="Button" HorizontalAlignment="Left" Height="62" Margin="10,0,0,0" VerticalAlignment="Top" Width="138" />

    <Path Data="M0,0L32,0 32,32 0,32z" Stretch="Uniform" Fill="Red" Margin="-167,62,-76,0" RenderTransformOrigin="0.5,0.5"  PreviewMouseLeftButtonUp="Path_PreviewMouseLeftButtonUp">
        <Path.RenderTransform>
            <TransformGroup>
                <TransformGroup.Children>
                    <RotateTransform Angle="0" />
                    <ScaleTransform ScaleX="1" ScaleY="1" />
                </TransformGroup.Children>
            </TransformGroup>
        </Path.RenderTransform>
    </Path>

</Grid>

The code behind for the MainWindow:

Class MainWindow


Private Sub button_Click(sender As Object, e As RoutedEventArgs) Handles button.Click
    Dim win As New dialog
    win.ShowDialog()
End Sub



Private Sub Path_MouseLeftButtonUp(sender As Object, e As MouseButtonEventArgs)

End Sub

Private Sub Path_PreviewMouseLeftButtonUp(sender As Object, e As MouseButtonEventArgs)
    MsgBox("ClickedPreview")
End Sub

End Class

The code for the dialog window:

<Window x:Class="dialog"
    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:DialogTest"
    mc:Ignorable="d"
    Title="dialog" Height="146" Width="132" WindowStartupLocation="CenterScreen">
<Grid>
    <Label x:Name="label" Content="Label" HorizontalAlignment="Center" Height="88" Margin="0,10,0,0" VerticalAlignment="Top" Width="112"/>

</Grid>

And the code behind for the dialog window:

Public Class dialog
Private Sub label_PreviewMouseLeftButtonDown(sender As Object, e As MouseButtonEventArgs) Handles label.PreviewMouseLeftButtonDown
    Me.Close()
End Sub

End Class

CodePudding user response:

Just letting anyone know I fixed this by changing the label to a button. The button click event doesn't fire the click event underneath.

  • Related