在 WPF 中,如何在不使用任何事件的情况下判断鼠标左键当前是否按下?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16085373/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 08:37:34  来源:igfitidea点击:

In WPF, how do you tell if the left mouse button is currently down without using any events?

c#wpfmousemouseleftbuttondown

提问by Curtis

I have an app where I want to be able to move a slider. However, the slider is automatically updated by the program every 30 seconds. When I try to change the slider position with my mouse, the program is still updating the slider position, so I cannot move it manually with the mouse.

我有一个应用程序,我希望能够在其中移动滑块。但是,滑块每 30 秒由程序自动更新一次。当我尝试用鼠标更改滑块位置时,程序仍在更新滑块位置,因此我无法用鼠标手动移动它。

Somehow, I want the app to NOT update the slider if the user is in the process of changing its position. How can I tell if the left mouse button is down without using the left button down event? I just want to be able to access the mouse, and check the button state of the left mouse button. How do I access it without being inside of a mouse event?

不知何故,如果用户正在更改其位置,我希望应用程序不更新滑块。如何在不使用左键按下事件的情况下判断鼠标左键是否按下?我只想能够访问鼠标,并检查鼠标左键的按钮状态。如何在不处于鼠标事件内部的情况下访问它?

回答by DanW

bool mouseIsDown = System.Windows.Input.Mouse.LeftButton == MouseButtonState.Pressed;

bool mouseIsDown = System.Windows.Input.Mouse.LeftButton == MouseButtonState.Pressed;

回答by Curtis

Ok,

好的,

I figured it out. The Slider control in WPF does NOT fire the MouseDown, LeftMouseDown, or LeftMouseUp events. This is because it uses them internally to adjust the value as the user is manipulating the slider. However, the Slider control in WPF DOES fire the PreviewLeftMouseDown and the PreviewLeftMouseUp events. In addition, when you click the Left Mouse button, the Slider Control automatically captures the mouse and holds on to it until you release the Left Mouse button.

我想到了。WPF 中的 Slider 控件不会触发 MouseDown、LeftMouseDown 或 LeftMouseUp 事件。这是因为它在用户操作滑块时在内部使用它们来调整值。但是,WPF 中的 Slider 控件确实会触发 PreviewLeftMouseDown 和 PreviewLeftMouseUp 事件。此外,当您单击鼠标左键时,滑块控件会自动捕获鼠标并按住鼠标,直到您松开鼠标左键。

I was able to solve my problem with the following 3 events:

我能够通过以下 3 个事件解决我的问题:

   private void _sliderVideoPosition_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
    {
        _adjustingVideoPositionSlider = true;
        _mediaElement.Pause();
    }

    private void _sliderVideoPosition_PreviewMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
    {
        _adjustingVideoPositionSlider = false;
        _mediaElement.Play();
    }

    private void _sliderVideoPosition_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
    {
        if (_adjustingVideoPositionSlider)
        {
            _mediaElement.Position = TimeSpan.FromMilliseconds((int)e.NewValue);
        }
    }

回答by Tigran

You may consider, in your case, using Mouse Class.

在您的情况下,您可以考虑使用Mouse Class

If I'm not mistaken it doesn't track the state of the button when the mouse goes out of the UI of the application, so to be sure that you have always correct information, you may need to capture mouse.

如果我没记错的话,当鼠标离开应用程序的 UI 时它不会跟踪按钮的状态,因此为了确保您始终拥有正确的信息,您可能需要捕获鼠标。

Repeat, you need to check this by yourself.

重复,你需要自己检查。

回答by Nick Freeman

As I understand it, you want to keep an MVVM pattern to accomplish this task.

据我了解,您希望保留 MVVM 模式来完成此任务。

So first I would start by designing what I want in my ViewModel

所以首先我会从设计我想要的 ViewModel 开始

public class ViewModel
{
    public ICommand EditingSliderCommand { get; set; } // actually set the command
    public ICommand DoneEditingCommand { get; set; } // actually set the command
    public bool IsEditing { get; set; }

    ...

    private void AutomaticUpdate
    {
        if (IsEditing)
            return;
        Update();
    }
}

then you could use the Blend Interactivitylibrary to do the UI side in xaml.

那么您可以使用该Blend Interactivity库在 xaml 中执行 UI 方面。

The following is an example. I don't know if the event name is correct. You could also do the same with mouse up and done editing.

下面是一个例子。我不知道事件名称是否正确。您也可以通过鼠标抬起并完成编辑来执行相同的操作。

<i:Interaction.Triggers>
    <i:EventTrigger EventName="MouseDown">
        <cmd:EventToCommand Command="{Binding Mode=OneWay, 
                            Path=EditingSliderCommand}"/>
    </i:EventTrigger>
</i:Interaction.Triggers>