Tip

Changing the cursor in WPF

The cursor is the visible link between the computer mouse and your UI. I know that I rarely even notice the cursor while working on the computer since using it is second nature to me. It is common to swap the cursor to signify UI changes. Show a wait cursor is a simple way to the user that the computer is busy. Likewise, show a cross hair cursor usually indicates a drawing tool is active.

Setting a cursor

In Windows Presentation Foundation (WPF) you can set the mouse pointer for any element using the Cursor property. This property is available for any FrameworkElement derived type, which includes Controls, Shapes and Panels.

The simplest way to provide a different Cursor is set the value in XAML.

This XAML results in the following cursor change.

Setting a cursor in code

The equivalent way to do this in code is by using the static properties of the Cursors class. Here's an example in C#.

Button1.Cursor = Cursors.Hand;

Using Visual Studio tools

If you use the Visual Studio then you will get IntelliSense as shown in the following screenshot.

Also, you can set the cursor in the designer view by choosing a value in the Property window.

Nesting cursors

It's conceivable that you might want to overlap cursor settings. Nesting cursors works in a hierarchical fashion. When you set a cursor for a parent element that cursor applies to all children unless you override it at a specific child element level. In the following example the 'Show Menubar 'checkbox doesn't have its own cursor so it will use the wait cursor specified in its parent Canvas.

Forcing a cursor on children

There might be times when you don't want a child element to override the parent. In these situations you can use the ForceCursor property. When this property is set to true WPF will ignore any child specific cursor settings.

Custom cursors

WPF ships with approximately 30 built-in cursors. If none of these fit your situation you can always add your own custom cursor files to your project. WPF support both animated cursors (*.ani) and static cursors (*.cur).  Simply add the cursor file to your project and marked it as a resource.

To load the cursor you'll use the Application.GetResourceStream() method. The following code shows how to accomplish this.

Notice the pack URI syntax in the previous example.  Using pack://application:,,, causes WPF to load the resource from the local assembly. Some of you are probably thinking, what's the deal with those three commas in the URI? They are needed because there is an embedded URI within the URI.  Application:// cannot be placed in the Pack URI without escaping the forward slash. So those three commas represent the two //.   The rest of the URI is the path to the nav.cur file. 

It's possible to put the cursor in a shared DLL. In ordered to retrieve a resource in a different assembly you have to use a slightly different pack URI string. The following code shows how to achieve this.

 

This was first published in January 2011

There are Comments. Add yours.

 
TIP: Want to include a code block in your comment? Use <pre> or <code> tags around the desired text. Ex: <code>insert code</code>

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy
Sort by: OldestNewest

Forgot Password?

No problem! Submit your e-mail address below. We'll send you an email containing your password.

Your password has been sent to:

Disclaimer: Our Tips Exchange is a forum for you to share technical advice and expertise with your peers and to learn from other enterprise IT professionals. TechTarget provides the infrastructure to facilitate this sharing of information. However, we cannot guarantee the accuracy or validity of the material submitted. You agree that your use of the Ask The Expert services and your reliance on any questions, answers, information or other materials received through this Web site is at your own risk.