Master
Master

Reputation: 2153

How can I change the Mouse Cursor over a Button?

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.

Xaml Side:

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

Upvotes: 3

Views: 8086

Answers (4)

Fawaz
Fawaz

Reputation: 3560

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

Instead when loading your App call this :

Mouse.OverrideCursor = null;

and everywhere call this :

<Button Cursor="Hand"/>

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

Upvotes: 2

Anatoliy Nikolaev
Anatoliy Nikolaev

Reputation: 22702

You do not need to create a event handler for this. Just add to the Style of your Button this trigger:

<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.

Upvotes: 12

aloisdg
aloisdg

Reputation: 23521

To change the cursor, you can use 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)

// 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.

Upvotes: 3

ryrich
ryrich

Reputation: 2204

In xaml try

<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;
}

Upvotes: 1

Related Questions