简体   繁体   English

WPF中的可拖动的无边界窗口和按钮单击处理程序

[英]Draggable Borderless Window and Button click handler in WPF

The Button handler doesnt work because of the DragMove call in Window mouse down handler. 由于Windows鼠标按下处理程序中的DragMove调用,因此Button处理程序不起作用。 Is there any way how to let the events bubbling? 有什么办法让事件冒泡吗? I tried set up e.Handled to false, but it does not work. 我尝试将e.Handled设置为false,但是它不起作用。

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

    private void Window_MouseDown(object sender, MouseButtonEventArgs e)
    {    
        this.DragMove();
        e.Handled = false;
    }   

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        System.Console.WriteLine("zde");
    }
}

The Button handler doesnt work because of the DragMove call

Actually, it doesn't work, because the first fires is an event at the Button.Click , and when it works, it conflicts with the events like: MouseLeftButtonDown, MouseUp, MouseDown and for routed events e.Handled property is false by default. 实际上,这是行不通的,因为第一次触发是Button.Click一个事件,当它起作用时,它会与以下事件发生冲突:MouseLeftButtonDown,MouseUp,MouseDown和路由事件e.Handled属性默认为false

To make this work, you need to define an PreviewMouseDown event, but it's a Tunnel event, this means that it will go down of the VisualTree hierarchy, therefore it is triggered before the Bubble events. 要使此工作正常进行,您需要定义一个PreviewMouseDown事件,但这是一个Tunnel事件,这意味着它将进入VisualTree层次结构,因此它在Bubble事件之前触发。

Example: 例:

XAML

<Window x:Class="MyProject.MainWindow"
        ...
        PreviewMouseDown="Window_PreviewMouseDown" ... />

Code-behind

private void Window_PreviewMouseDown(object sender, MouseButtonEventArgs e)
{
    if (e.ChangedButton == MouseButton.Left)
        this.DragMove();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM