FrameworkElement.Cursor FrameworkElement.Cursor FrameworkElement.Cursor FrameworkElement.Cursor Property

定义

获取或设置在鼠标指针位于此元素上时显示的光标。Gets or sets the cursor that displays when the mouse pointer is over this element.

public:
 property System::Windows::Input::Cursor ^ Cursor { System::Windows::Input::Cursor ^ get(); void set(System::Windows::Input::Cursor ^ value); };
public System.Windows.Input.Cursor Cursor { get; set; }
member this.Cursor : System.Windows.Input.Cursor with get, set
Public Property Cursor As Cursor

属性值

要显示的光标。The cursor to display. 按此依赖属性,默认值定义为 nullThe default value is defined as null per this dependency property. 但是,运行时的实际默认值将受到多种因素的影响。However, the practical default at run time will come from a variety of factors.

示例

下面的示例演示如何特意设置光标图形。The following example shows how to deliberately set the cursor graphic.

private void CursorTypeChanged(object sender, SelectionChangedEventArgs e)
{
    ComboBox source = e.Source as ComboBox;

    if (source != null)
    {
        ComboBoxItem selectedCursor = source.SelectedItem as ComboBoxItem;

        // Changing the cursor of the Border control 
        // by setting the Cursor property
        switch (selectedCursor.Content.ToString())
        {
            case "AppStarting":
                DisplayArea.Cursor = Cursors.AppStarting;
                break;
            case "ArrowCD":                        
                DisplayArea.Cursor = Cursors.ArrowCD;
                break;
            case "Arrow":
                DisplayArea.Cursor = Cursors.Arrow;
                break;
            case "Cross":
                DisplayArea.Cursor = Cursors.Cross;
                break;
            case "HandCursor":
                DisplayArea.Cursor = Cursors.Hand;
                break;
            case "Help":
                DisplayArea.Cursor = Cursors.Help;
                break;
            case "IBeam":
                DisplayArea.Cursor = Cursors.IBeam;
                break;
            case "No":
                DisplayArea.Cursor = Cursors.No;
                break;
            case "None":
                DisplayArea.Cursor = Cursors.None;
                break;
            case "Pen":
                DisplayArea.Cursor = Cursors.Pen;
                break;
            case "ScrollSE":
                DisplayArea.Cursor = Cursors.ScrollSE;
                break;
            case "ScrollWE":
                DisplayArea.Cursor = Cursors.ScrollWE;
                break;
            case "SizeAll":
                DisplayArea.Cursor = Cursors.SizeAll;
                break;
            case "SizeNESW":
                DisplayArea.Cursor = Cursors.SizeNESW;
                break;
            case "SizeNS":
                DisplayArea.Cursor = Cursors.SizeNS;
                break;
            case "SizeNWSE":
                DisplayArea.Cursor = Cursors.SizeNWSE;
                break;
            case "SizeWE":
                DisplayArea.Cursor = Cursors.SizeWE;
                break;
            case "UpArrow":
                DisplayArea.Cursor = Cursors.UpArrow;
                break;
            case "WaitCursor":
                DisplayArea.Cursor = Cursors.Wait;
                break;
            case "Custom":
                DisplayArea.Cursor = CustomCursor;
                break;
            default:
                break;
        }

        // If the cursor scope is set to the entire application
        // Use OverrideCursor to force the cursor for all elements
        if (cursorScopeElementOnly == false)
        {
            Mouse.OverrideCursor = DisplayArea.Cursor;
        }
    }
}
' When the Radiobox changes, a new cursor type is set
Private Sub CursorTypeChanged(ByVal sender As Object, ByVal e As SelectionChangedEventArgs)

    Dim item As String = CType(e.Source, ComboBox).SelectedItem.Content.ToString()

    Select Case item
        Case "AppStarting"
            DisplayArea.Cursor = Cursors.AppStarting
        Case "ArrowCD"
            DisplayArea.Cursor = Cursors.ArrowCD
        Case "Arrow"
            DisplayArea.Cursor = Cursors.Arrow
        Case "Cross"
            DisplayArea.Cursor = Cursors.Cross
        Case "HandCursor"
            DisplayArea.Cursor = Cursors.Hand
        Case "Help"
            DisplayArea.Cursor = Cursors.Help
        Case "IBeam"
            DisplayArea.Cursor = Cursors.IBeam
        Case "No"
            DisplayArea.Cursor = Cursors.No
        Case "None"
            DisplayArea.Cursor = Cursors.None
        Case "Pen"
            DisplayArea.Cursor = Cursors.Pen
        Case "ScrollSE"
            DisplayArea.Cursor = Cursors.ScrollSE
        Case "ScrollWE"
            DisplayArea.Cursor = Cursors.ScrollWE
        Case "SizeAll"
            DisplayArea.Cursor = Cursors.SizeAll
        Case "SizeNESW"
            DisplayArea.Cursor = Cursors.SizeNESW
        Case "SizeNS"
            DisplayArea.Cursor = Cursors.SizeNS
        Case "SizeNWSE"
            DisplayArea.Cursor = Cursors.SizeNWSE
        Case "SizeWE"
            DisplayArea.Cursor = Cursors.SizeWE
        Case "UpArrow"
            DisplayArea.Cursor = Cursors.UpArrow
        Case "WaitCursor"
            DisplayArea.Cursor = Cursors.Wait
        Case "Custom"
            DisplayArea.Cursor = CustomCursor
    End Select

    ' if the cursor scope is set to the entire application
    ' use OverrideCursor to force the cursor for all elements
    If (cursorScopeElementOnly = False) Then
        Mouse.OverrideCursor = DisplayArea.Cursor
    End If


End Sub

注解

在中XAMLXAML设置此属性时XAMLXAML , 处理器依赖于Cursor类的类型转换来计算字符串。When you set this property in XAMLXAML, the XAMLXAML processor relies on type conversion for the Cursor class to evaluate the string. 提供的字符串的计算结果应CursorType为值。The provided string should evaluate to a CursorType value. 有关详细信息,请参阅CursorSee Cursor for details.

当鼠标指针位于此元素上时, 是否显示此属性所建立的光标也依赖于该ForceCursor属性的值。Whether the cursor as established by this property will or will not display when the mouse pointer is over this element is also dependent on the value of the ForceCursor property. 此外, 与事件相关的注意事项, 如活动拖动、鼠标捕获、控件内的文本编辑模式等, 也会影响其优先级高于在此属性中指定的值的光标。Also, event-related considerations such as an active drag, mouse capture, text editing modes within controls, and so on, will also affect the cursor with higher priority than the value you specify in this property.

若要将此属性的设置方式恢复为最终的默认值, 请null将其重新设置为。To revert the behavior of setting this property to the eventual default, set it to null again.

null默认情况下, 这意味着确定实际游标值是在此处推迟的, 应从其他位置获得。The null default really means that determination of the practical cursor value is deferred here and should be obtained from elsewhere. 如果未从任何源以编程方式提供值, 则会在Windows Presentation Foundation (WPF)Windows Presentation Foundation (WPF)应用程序上直观地显示默认光标。If presented without programmatic values from any source, the default cursor that is visually over a Windows Presentation Foundation (WPF)Windows Presentation Foundation (WPF) application will be an arrow. 但是, 在传递时, 不会将暂时性游标Cursor更改设置为元素的值。However, the transient cursor changes are not set to the Cursor values of the elements when they are passed over. 在实际设置属性的情况下 (例如通过代码或样式), 属性只会报告非 null 值。 CursorThe Cursor property will only report non null values in cases where it was actually set, for instance through code or a style. 鼠标在WPFWPF应用程序上的每个移动都QueryCursor将引发一个事件。Each movement of the mouse over a WPFWPF application raises a QueryCursor event. 事件冒泡, 而路由中的任何元素都有机会处理事件, 并通过此事件的参数设置游标的值。The event bubbles, and any element along the route has the opportunity to handle the event and to set the value of the cursor through the arguments of this event. 这是在大多数情况下生成视觉上明显的光标的机制。This is the mechanism that produces the visually apparent cursor in most cases. 如果处理程序返回游标结果, 则处理事件并在参数中具有更改的值将优先于任何级别的Cursor属性的值, 除非ForceCursor已设置。 QueryCursorIf a QueryCursor handler returns a cursor result, then the fact that the event is handled and has a changed value in the arguments takes precedence over the value of the Cursor property at any level, unless ForceCursor is set.

如果未创建自定义游标, 则通常会将此属性设置为Cursors类的静态属性值。If not are not creating a custom cursor, you typically set this property to a static property value of the Cursors class. Cursor代码中设置需要执行下列操作之一:Setting Cursor in code requires one of the following:

在部分信任环境中未启用将设置为自定义值。CursorSetting the Cursor to a custom value is not enabled in partial trust. 有关自定义游标的详细信息, 请参阅输入概述For more information on custom cursors, see Input Overview.

依赖项属性信息Dependency Property Information

标识符字段Identifier field CursorProperty
元数据属性设置为trueMetadata properties set to true NoneNone

适用于

另请参阅