【问题标题】:ShowDialog Allowing Event Triggers on MainWindow Underneath WPF VB.NETShowDialog 允许在 WPF VB.NET 下的 MainWindow 上触发事件
【发布时间】:2022-01-09 03:55:39
【问题描述】:

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

这是我的 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>

主窗口背后的代码:

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

结束类

对话窗口的代码:

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

以及对话窗口背后的代码:

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

结束类

【问题讨论】:

    标签: wpf vb.net


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 2013-08-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多