ContentElement.Focus ContentElement.Focus ContentElement.Focus ContentElement.Focus Method

定義

嘗試將焦點設定至此項目。Attempts to set focus to this element.

public:
 virtual bool Focus();
public bool Focus ();
abstract member Focus : unit -> bool
override this.Focus : unit -> bool
Public Function Focus () As Boolean

傳回

如果可將鍵盤焦點設定至此項目則為 true,如果這個方法無法強制設定焦點則為 falsetrue if keyboard focus could be set to this element; false if this method call did not force focus.

實作

範例

下列範例是頁面載入的事件處理常式, 可在檔中尋找指定的已命名段落, 並將焦點設定到該專案。The following example is a page-loaded event handler that finds a specified named paragraph in the document and sets focus to it. 段落預設為不可設定焦點;這個特定段落已套用樣式 (未顯示), 其使用樣式Setter來使其成為可設定焦點。Paragraphs are not focusable by default; this particular paragraph had a style applied (not shown) that used a style Setter to make it focusable.

void FocusOnParagraph(object sender, RoutedEventArgs e)
{
  ContentElement ce = this.FindName("focusableP") as ContentElement;
  ce.Focus();
}
Private Sub FocusOnParagraph(ByVal sender As Object, ByVal e As RoutedEventArgs)
  Dim ce As ContentElement = TryCast(Me.FindName("focusableP"), ContentElement)
  ce.Focus()
End Sub

備註

要成為可設定Focusable焦點IsEnabled的, 而且true兩者都必須是。To be focusable, Focusable and IsEnabled must both be true. 請注意, 幾乎ContentElement所有衍生的類別Focusable都不是預設值。Note that nearly all ContentElement derived classes are not Focusable by default.

即使專案具有可設定焦點並已啟用, 在特定樹狀結構內的事件處理 (例如複合控制項) 可能會因不允許焦點而回應預覽焦點事件, 因此這個方法會false傳回。Even if an element is focusable and enabled, event handling within a specific tree, (such as for a composite control) might respond to the preview focus events by not allowing focus there, thus this method would return false.

一般焦點是由兩個不同的概念所控制: 鍵盤焦點和邏輯焦點, 這不一定完全相同。Focus in general is governed by two separate concepts: keyboard focus and logical focus, which are not always identical. 這個方法會設定邏輯焦點。This method sets the logical focus. 沒有程式設計的方法可以特別設定鍵盤焦點;鍵盤焦點是由使用者輸入所決定。There is no programmatic means to set keyboard focus specifically; keyboard focus is determined by user input. 如需詳細資訊, 請參閱焦點總覽輸入總覽For more information, see Focus Overview and Input Overview.

如果呼叫Focus true傳回, IsKeyboardFocusedIsKeyboardFocusWithin也是。trueIf calling Focus returns true, IsKeyboardFocused and IsKeyboardFocusWithin are also true.

如果沒有true相關的屬性, 當您呼叫Focus時, 會以下列順序引發下列一個或多個事件: PreviewLostKeyboardFocusPreviewGotKeyboardFocus (來源是新的焦點目標)、 IsKeyboardFocusedChangedIsKeyboardFocusWithinChangedLostKeyboardFocus ,GotKeyboardFocus (source 是新的焦點目標)。If the related properties are not already true, when you call Focus, one or more of the following events are raised in the following order: PreviewLostKeyboardFocus, PreviewGotKeyboardFocus (source is the new focus target), IsKeyboardFocusedChanged, IsKeyboardFocusWithinChanged, LostKeyboardFocus, GotKeyboardFocus (source is the new focus target).

適用於

另請參閱