简体   繁体   English

使用DragMove方法在WPF中拖动窗口,然后单击同一按钮上的处理程序

[英]Dragging a window in WPF using DragMove method and click handler on the same button

I need a button for two purposes: 我需要一个按钮用于两个目的:
- Users can drag the application's window using the button -用户可以使用按钮拖动应用程序的窗口
- Users can simply click the button to toggle visibility of some other element in the window. -用户只需单击按钮即可切换窗口中其他元素的可见性。

The button is a PNG image. 该按钮是PNG图片。

I am trying to do it in the following way: 我正在尝试通过以下方式做到这一点:

XAML: XAML:

<Button Name="toggleButton" Click="toggleButton_Click" Canvas.Left="177" Canvas.Top="0">
  <Button.Template>
    <ControlTemplate>
      <Image Source="/FootballRssReader;component/images/ball.png" MouseLeftButtonDown="toggleButton_MouseLeftButtonDown"/>
    </ControlTemplate>
  </Button.Template>
</Button>

C#: C#:

 private void toggleButton_Click(object sender, RoutedEventArgs e)
        {
            contentVisible = !contentVisible;
            content.Visibility = contentVisible ? Visibility.Visible : Visibility.Collapsed;
        }

 private void toggleButton_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            DragMove();
        }

The problem is that only the window moving works. 问题在于只有窗口移动才有效。 Clicks on the button don't invoke the Click event handler. 单击按钮不会调用Click事件处理程序。 When I remove MouseLeftButtonDown event handling from the button's image, the Click event is executed. 当我从按钮的图像中删除MouseLeftButtonDown事件处理时,将执行Click事件。

Can anybody help me? 有谁能够帮助我? Is it possible to create such a button? 是否可以创建这样的按钮?

I tried setting Handled to false in the Image but it didn't help. 我尝试在Image中将Handled设置为false,但这没有帮助。

Thanks, Michal 谢谢,米哈尔

DragMove starts a modal message loop and doesn't return until the mouse button is released, so by the time the button receives the MouseLeftButtonDown event it's already lost the chance to click. DragMove启动模式消息循环,直到释放鼠标按钮时才返回,因此当按钮收到MouseLeftButtonDown事件时,它已经失去了单击的机会。

I'm assuming you don't want the Click to happen if the user drags the window. 我假设您不希望用户拖动窗口时发生单击。 One approach is to do something similar to drag-drop, and only call DragMove if the mouse starts moving while it is pressed. 一种方法是执行类似于拖放的操作,并且仅在鼠标按下时开始移动时才调用DragMove。 Attach handlers to PreviewMouseLeftButtonDown and PreviewMouseMove on the Button: 将处理程序附加到Button上的PreviewMouseLeftButtonDown和PreviewMouseMove:

<Button Name="toggleButton" Click="toggleButton_Click"
    Canvas.Left="177" Canvas.Top="0"
    PreviewMouseMove="toggleButton_PreviewMouseMove"
    PreviewMouseLeftButtonDown="toggleButton_PreviewMouseLeftButtonDown">
    <Button.Template>
        <ControlTemplate>
            <Image Source="/FootballRssReader;component/images/ball.png"/>
        </ControlTemplate>
    </Button.Template>
</Button>

Record the mouse position in the PreviewLeftMouseButtonDown handler, and then start the DragMove in the PreviewMouseMove handler if the mouse has started moving: 在PreviewLeftMouseButtonDown处理程序中记录鼠标的位置,如果鼠标已经开始移动,则在PreviewMouseMove处理程序中启动DragMove:

private Point startPoint;

private void toggleButton_PreviewMouseLeftButtonDown(
    object sender, MouseButtonEventArgs e)
{
    startPoint = e.GetPosition(toggleButton);
}

private void toggleButton_PreviewMouseMove(object sender, MouseEventArgs e)
{
    var currentPoint = e.GetPosition(toggleButton);
    if (e.LeftButton == MouseButtonState.Pressed &&
        toggleButton.IsMouseCaptured &&
        (Math.Abs(currentPoint.X - startPoint.X) >
            SystemParameters.MinimumHorizontalDragDistance ||
        Math.Abs(currentPoint.Y - startPoint.Y) >
            SystemParameters.MinimumVerticalDragDistance))
    {
        // Prevent Click from firing
        toggleButton.ReleaseMouseCapture();
        DragMove();
    }
}

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

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