The cursor is the visible link between the computer mouse and your UI. I know that I rarely even notice the cursor...
By submitting your email address, you agree to receive emails regarding relevant topic offers from TechTarget and its partners. You can withdraw your consent at any time. Contact TechTarget at 275 Grove Street, Newton, MA.
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.
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.
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.