Page view counter

Silverlight Tips of the Day - Blog by Mike Snow

Game Programming with Silverlight

Silverlight Tip of the Day #27: How to Change the Mouse Cursor

Let’s say you want to change the mouse cursor when hovering over a UI element. You can do this directly in the XAML by setting the Cursor property.
For example, if you want to change the cursor to be the Hand image    cursor when hovering over a button the XAML you would use would be something like this:

<Canvas >
    <Button Cursor="Hand" Width="100" Height="50" Content="Hover over me"></Button>
</Canvas>

When running the app you would see, as in the screen shot below, the mouse cursor change from the arrow to the hand.

image

To do this programmatically you could place a MouseEnter and MouseLeave event handler on the button.

<Grid x:Name="LayoutRoot" Background="White">
    <Canvas >
        <Button x:Name="myButton" MouseEnter="Button_MouseEnter" MouseLeave="Button_MouseLeave" Width="100" Height="50" Content="Hover over me"></Button>
    </Canvas>
</Grid>

Then, in the event handler you could change the cursor this way:

private void Button_MouseEnter(object sender, MouseEventArgs e)
{
    myButton.Cursor = Cursors.Hand;
}
 
private void Button_MouseLeave(object sender, MouseEventArgs e)
{
    myButton.Cursor = Cursors.Arrow;
}

Silverlight provides the following cursors through the Cursor object:

  1. Arrow 
  2. Eraser
  3. Hand 
  4. IBeam
  5. SizeNS
  6. SizeWE 
  7. Stylus
  8. Wait

Thank you,
--Mike Snow

 Subscribe in a reader

Comments

Visual Web Developer Team Blog said:

7 new Silverlight blogs have been completed. Check them out and let me know if you have any questions.

# August 14, 2008 2:51 PM

Microsoft Weblogs said:

Let’s say you want to change the mouse cursor when hovering over a UI element. You can do this directly

# August 14, 2008 3:25 PM

carnewsservice.info » Tip of the Day #27: How to Change the Mouse Cursor said:

Pingback from  carnewsservice.info &raquo; Tip of the Day #27: How to Change the Mouse Cursor

# August 14, 2008 3:45 PM

Community Blogs said:

SilverlightOnly Panorama Viewer, Alex Golesh on Isolated Storage, Mike Snow on moving the XAP, Changing

# August 15, 2008 2:16 AM

Dew Drop - August 15, 2008 | Alvin Ashcraft's Morning Dew said:

Pingback from  Dew Drop - August 15, 2008 | Alvin Ashcraft's Morning Dew

# August 15, 2008 9:43 AM

Silverlight news for August 15, 2008 said:

Pingback from  Silverlight news for August 15, 2008

# August 15, 2008 12:34 PM

emc11 said:

How would one accomplish this effect using a custom cursor? I didn't see the cursor object accepting any parameters to use a custom resource or graphic.

Thanks!

# August 15, 2008 3:52 PM

Mike Snows Silverlight Blog said:

In Tip of the Day #27 we talked about how to change the mouse cursor. But what if you want to have a

# August 18, 2008 2:27 PM

mike.snow said:

Emc11- See my latest tutorial on how to do this:

silverlight.net/.../tip-of-the-day-28-how-to-implement-a-custom-mouse-cursor.aspx

Thanks,

--Mike

# August 18, 2008 2:28 PM

Silverlight Tips of the Day - Blog by Mike Snow said:

The purpose of this post is to create an outline summary all the blogs from my Silverlight tips of the

# January 2, 2009 5:56 PM

Silverlight Tips of the Day - Blog by Mike Snow said:

The purpose of this post is to create an outline summary all the blogs from my Silverlight tips of the

# January 2, 2009 5:56 PM

o UAU nosso de cada dia said:

essa lista eu copiei desse blog bárbaro (acompanhe por RSS você também): uma lista de dicas super úteis

# January 3, 2009 6:25 AM

Silverlight Tips of the Day said:

In Tip of the Day #27 we talked about how to change the mouse cursor. But what if you want to have a

# January 14, 2009 6:22 PM

Silverlight Tips of the Day - Blog by Mike Snow said:

In Tip of the Day #27 we talked about how to change the mouse cursor. But what if you want to have a

# March 31, 2009 7:33 PM

klaywilliams said:

Why do you need to use MouseEnter/Leave?  To do this programmatically, can't you just say:

myButton.Cursor = Cursors.Hand;

If you aren't hovering over the button, the cursor is automatically set to the default.

# May 22, 2009 11:09 AM

LetsBlogAbout.NET said:

Silverlight Tips of the Day

# October 15, 2010 11:07 AM

Mouse over pointer - Mouse move - Mouse pointer said:

Pingback from  Mouse over pointer - Mouse move - Mouse pointer

# November 6, 2010 2:06 PM