wpf 如何更改按钮上的鼠标光标?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22720381/
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 11:23:09  来源:igfitidea点击:

How can I change the Mouse Cursor over a Button?

c#wpfxaml

提问by Master

I'm currently Implementing Caliburn and having a mouseover implementation. I'm wondering how do I change the mouse cursor to the mouse on button.

我目前正在实施 Caliburn 并进行鼠标悬停实施。我想知道如何将鼠标光标更改为鼠标按钮。

Xaml Side:

Xaml 端:

<Button cal:Message.Attach="[Event MouseOver] = [ChangeIcon]" />

回答by Anatoliy Nikolaev

You do not need to create a event handler for this. Just add to the Styleof your Buttonthis trigger:

您不需要为此创建事件处理程序。只需添加到Style您的Button此触发器中:

<Style TargetType="{x:Type Button}">
    <Style.Triggers>
        <Trigger Property="IsMouseOver" Value="True">
            <Setter Property="Cursor" Value="Wait" />
        </Trigger>
    </Style.Triggers>
</Style>

Besides, the Cursor and the Mouse event is related to a View. This means that it is desirable to do this action in not the ViewModel, and do it on the side of View.

此外,Cursor 和 Mouse 事件与View. 这意味着最好在不做这个动作ViewModel,并在旁边做View

回答by aloisdg moving to codidact.com

To change the cursor, you can use Mouse.OverrideCursor Property

要更改光标,您可以使用 Mouse.OverrideCursor Property

private void CursorTypeChanged(object sender, SelectionChangedEventArgs e)
{
    Mouse.OverrideCursor = Cursors.Wait;
}

Then to reset, you can use :

然后要重置,您可以使用:

Mouse.OverrideCursor = null;

another example (from msdn)

另一个例子(来自msdn

// Determines the scope the new cursor will have. 
// 
// If the RadioButton rbScopeElement is selected, then the cursor 
// will only change on the display element. 
//  
// If the Radiobutton rbScopeApplication is selected, then the cursor 
// will be changed for the entire application 
// 
private void CursorScopeSelected(object sender, RoutedEventArgs e)
{
    RadioButton source = e.Source as RadioButton;

    if (source != null)
    {
        if (source.Name == "rbScopeElement")
        {
            // Setting the element only scope flag to true
            cursorScopeElementOnly = true;

            // Clearing out the OverrideCursor.  
            Mouse.OverrideCursor = null;
        }
        if (source.Name == "rbScopeApplication")
        {
           // Setting the element only scope flag to false
           cursorScopeElementOnly = false;

           // Forcing the cursor for all elements. 
           Mouse.OverrideCursor = DisplayArea.Cursor;
        }
    }
}

Need more info ?

需要更多信息?

  • List of properties of cursor class : msdn
  • There is a great "How to" on msdn.
  • 游标类的属性列表:msdn
  • msdn上有一个很棒的“操作方法” 。

回答by Fawaz

Don't use Mouse.OverrideCursor anywhere otherwise Cursor="Hand" will not work.

不要在任何地方使用 Mouse.OverrideCursor 否则 Cursor="Hand" 将不起作用。

Instead when loading your App call this :

而是在加载您的应用程序时调用:

Mouse.OverrideCursor = null;

Mouse.OverrideCursor = null;

and everywhere call this :

到处都这样称呼:

<Button Cursor="Hand"/>

<Button Cursor="Hand"/>

Using Mouse.OverrideCursor will set the cursor for entire App Space!

使用 Mouse.OverrideCursor 将为整个 App Space 设置光标!

回答by ryrich

In xaml try

在 xaml 中尝试

<Button x:Name="btn" MouseEnter="btn_OnMouseEnter" MouseLeave="btn_OnMouseLeave" />

in code behind

在后面的代码中

private void btn_OnMouseEnter(object sender, MouseEventArgs e)
{
    // example. you can change to a specific type in the Cursors static class
    Cursor.Current = Cursors.WaitCursor;
}

private void btn_OnMouseLeave(object sender, MouseEventArgs e)
{
    Cursor.Current = Cursors.Default;
}