FrameworkElement.OnGotFocus(RoutedEventArgs) FrameworkElement.OnGotFocus(RoutedEventArgs) FrameworkElement.OnGotFocus(RoutedEventArgs) FrameworkElement.OnGotFocus(RoutedEventArgs) Method


當未處理的 GotFocus 事件到達其路由中的這個項目時,便會叫用此方法。Invoked whenever an unhandled GotFocus event reaches this element in its route.

 override void OnGotFocus(System::Windows::RoutedEventArgs ^ e);
protected override void OnGotFocus (System.Windows.RoutedEventArgs e);
override this.OnGotFocus : System.Windows.RoutedEventArgs -> unit
Protected Overrides Sub OnGotFocus (e As RoutedEventArgs)


RoutedEventArgs RoutedEventArgs RoutedEventArgs RoutedEventArgs

包含事件資料的 RoutedEventArgsThe RoutedEventArgs that contains the event data.


不同于基底專案所公開的 * 方法, OnGotFocus有一個預設的實值。Unlike some other On* methods exposed by the base elements, OnGotFocus does have a default implementation. 具體來說, 它有一個會覆寫下一個基底元素OnGotFocus層級中 null 實的實值。Specifically, it has an implementation that overrides the null implementation in the next level of base element down, OnGotFocus. 當叫用OnGotFocus時, 會在此專案上設定適當的焦點行為, 這是因為鍵盤焦點導致事件源自目前的專案。When invoked, OnGotFocus sets appropriate focus behavior on this element in cases where the event originated from the current element due to keyboard focus. 即使焦點設定為目前的專案,處理常式也不會將事件引數標示為已處理。OnGotFocusThe OnGotFocus handler does not mark the event arguments as handled, even when focus is set to the current element. 如果事件的來源是樹狀結構中的另一個元素 (而不是目前的專案), 則處理常式不會執行任何作業。If the event's source was another element in the tree (not the current element), the handler does nothing.

您可以覆寫這個方法, 以變更專案的預設焦點行為, 但請注意, 以這種方式變更焦點行為可能會因為不允許元素全部成為可供充分利用而達成 (請參閱Focusable)。You can override this method in order to change the default focus behavior on your element, but be aware that changing focus behavior in this way might be better accomplished by not allowing the element to be focusable at all (see Focusable).


如果您想要將引數中的事件標示為已處理, 您應該留意專案樹狀結構中其他父元素的事件處理結果。If you intend to mark the event handled in the arguments, you should be aware of consequences on event handling in other parent elements in the element tree. 因為此處理程式會在具有反升路由的事件上運作, 所以根據事件sender引數將焦點設定為目前可能不適用。Because this handler acts on an event with a bubbling routing, setting the focus to the current sender per the event arguments may not be appropriate. 視特定控制項的組合而定, 焦點可能需要移至複合子項目或父元素。Focus might need to go to either a composite child element or to a parent element, depending on the compositing of certain controls. 因此, 只有當事件所路由的整個視覺化樹狀結構是您所撰寫控制項的一部分時, 才建議將焦點事件標示為已處理。Therefore marking a focus event as handled is only recommended if the entirety of the visual tree that the event is routing through is part of compositing of a control that you authored.