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. Cursor屬性只會在實際設定 (例如透過程式碼或樣式) 的情況下, 報告非 null 值。The 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 None

適用於

另請參閱