如何在按钮上更改鼠标光标?

时间:2014-03-28 18:18:56

标签: c# wpf xaml

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

Xaml Side:

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

4 个答案:

答案 0 :(得分:10)

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

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

此外,Cursor和Mouse事件与View相关。这意味着需要在ViewModel中执行此操作,并在View一侧执行此操作。

答案 1 :(得分:3)

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

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

然后重置,您可以使用:

Mouse.OverrideCursor = null;

另一个例子(来自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;
        }
    }
}

需要更多信息?

  • 游标类​​的属性列表:msdn
  • 有一个伟大的&#34;如何&#34;在msdn

答案 2 :(得分:2)

不要在任何地方使用Mouse.OverrideCursor,否则Cursor =“Hand”将无效。

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

Mouse.OverrideCursor = null;

到处都叫这个:

<Button Cursor="Hand"/>

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

答案 3 :(得分:1)

在xaml尝试

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

中的代码

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;
}
相关问题