#709 – Changing the Cursor

You can change the image displayed as a cursor in your WPF application by setting the Cursor property of any FrameworkElement.    You can set the cursor to one of the predefined Cursor objects in the System.Windows.Input.Cursors class.

In the example below, we set the cursor to a hand whenever the user moves the mouse over the blue label. Setting the Cursor property to null resets it to the default cursor.

    <StackPanel Orientation="Vertical" HorizontalAlignment="Center" Margin="45">
        <Label Content="Hey, move your mouse over me !" Background="AliceBlue" Padding="15,10"
                MouseEnter="Label_MouseEnter" MouseLeave="Label_MouseLeave"/>
    </StackPanel>

 

        private void Label_MouseEnter(object sender, MouseEventArgs e)
        {
            this.Cursor = Cursors.Hand;
        }

        private void Label_MouseLeave(object sender, MouseEventArgs e)
        {
            this.Cursor = null;
        }

709-001
709-002

Advertisement

About Sean
Software developer in the Twin Cities area, passionate about software development and sailing.

4 Responses to #709 – Changing the Cursor

  1. Pingback: Dew Drop – December 11, 2012 (#1,460) | Alvin Ashcraft's Morning Dew

  2. Hi Sean. Just as a note, it’s not required to write code-behind to do that. Setting the Cursor property in XAML will have the same effect.

  3. Pingback: #829 – Setting an Application-Wide Cursor from Code | 2,000 Things You Should Know About WPF

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: