ShowDialog 允许在 WPF 下的 MainWindow 上触发事件 VB.NET

ShowDialog Allowing Event Triggers on MainWindow Underneath WPF VB.NET

我有一个主 window 和一个对话框 window。 mainwindow 有一条路径,带有 PreviewMouseLeftButtonUp 事件和一个按钮。该按钮为我的对话框 window 打开一个 'ShowDialog'。此对话框 window 上有一个标签,带有关闭对话框的 PreviewMouseLeftButtonDown 事件。对话框直接在路径上方打开。当我单击标签关闭对话框时,路径 PreviewMouseLeftButtonUp 事件也会触发。我该如何防止这种情况?我希望这是有道理的,感谢你能给我的任何帮助。

这是我的主窗口代码:

<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>

主窗口背后的代码:

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

结束Class

对话框的代码 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>

以及对话框的隐藏代码 window:

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

结束Class

只是让任何人知道我通过将标签更改为按钮来解决此问题。按钮点击事件不会触发下面的点击事件。