WPF鼠标事件从子窗口传播到所有者窗口

qyswt5oh  于 2023-10-22  发布在  其他
关注(0)|答案(1)|浏览(109)

我写了一段代码来描述我的问题:当点击主窗口上的一个控件时,MouseLeftButtonDown命令显示一个子窗口作为模态。当在子窗口中双击时,将触发主窗口中的MouseLeftButtonUp。如何避免这种情况没有一个标志?

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
    }

    private void MouseLeftButtonDownCommand(object sender, MouseButtonEventArgs e)
    {
        ChildWindow childWindow = new ChildWindow();
        childWindow.Owner = this;
        childWindow.ShowDialog();
    }

    private void MouseLeftButtonUpCommand(object sender, MouseButtonEventArgs e)
    {
        MessageBox.Show("Event Propagated to main window");
    }
}

主XAML

<Grid>
    <Canvas Background="Transparent" MouseLeftButtonDown="MouseLeftButtonDownCommand" MouseLeftButtonUp="MouseLeftButtonUpCommand">
        
    </Canvas>
</Grid>

酒店MODAL WINDOWS

public partial class ChildWindow : Window
{
    public ChildWindow()
    {
        InitializeComponent();
    }

    private void LeftDoubleClickCommand(object sender, MouseButtonEventArgs e)
    {
        this.Close();
    }
}

MODAL WINDOWS XAML

<Grid>
    <ListView Margin="10" MouseDoubleClick="LeftDoubleClickCommand">
        <ListViewItem Content="Coffie"></ListViewItem>
        <ListViewItem Content="Tea"></ListViewItem>
        <ListViewItem Content="Orange Juice"></ListViewItem>
        <ListViewItem Content="Milk"></ListViewItem>
        <ListViewItem Content="Iced Tea"></ListViewItem>
        <ListViewItem Content="Mango Shake"></ListViewItem>
    </ListView>
</Grid>
c3frrgcw

c3frrgcw1#

在消息循环为空之前,不要关闭对话框窗口:

private void LeftDoubleClickCommand(object sender, MouseButtonEventArgs e)
{
    Dispatcher.BeginInvoke(() => this.Close(), System.Windows.Threading.DispatcherPriority.Background);
}

或将事件标记为已处理:

private void LeftDoubleClickCommand(object sender, MouseButtonEventArgs e)
{
    e.Handled = true;
    this.Close();
}

相关问题