UIElement.IsFocused 属性


获取一个值,该值确定此元素是否具有逻辑焦点。Gets a value that determines whether this element has logical focus. 这是依赖项属性。This is a dependency property.

 property bool IsFocused { bool get(); };
public bool IsFocused { get; }
member this.IsFocused : bool
Public ReadOnly Property IsFocused As Boolean


如果此元素具有逻辑焦点,则为 true;否则为 falsetrue if this element has logical focus; otherwise, false.


下面的示例是一个代码处理程序,它在焦点为焦点时更改控件的背景。The following example is a code handler that changes the background of a control when it is focused.

private void setColors(object sender, RoutedEventArgs e)
    if (myTextBox.IsFocused) myTextBox.Foreground = Brushes.Brown;

实现相同效果的另一种常见方法是将 Trigger 放置在控件的 Style 中;此方法不需要使用代码隐藏来处理特定的事件,并允许设计人员更好地访问 UIUI的交互式和图形特性。Another common way to achieve this same effect is to place a Trigger in the Style of a control; this approach does not require handling specific events with code-behind and allows designers greater access to the interactive and graphical nature of the UIUI. 有关示例,请参阅如何:创建外部发光效果For an example, see How to: Create an Outer Glow Effect.


如果应用程序有多个焦点区域(例如菜单内容和应用程序的其余部分之间),则逻辑焦点可能与键盘焦点不同。Logical focus might differ from keyboard focus if an application has multiple focus divisions, such as between menu contents and the remainder of the application. 在此方案中,键盘焦点只能在应用程序 UI 的一个元素上,但是,其他焦点部门中的某些元素可能仍会保留逻辑焦点。In this scenario, keyboard focus can only be on one element of the application UI, however, certain elements in other focus divisions might still retain logical focus. 有关逻辑焦点的详细信息,请参阅焦点概述For more information on logical focus, see Focus Overview.

不通过设置此属性(它是只读的)来设置焦点。You do not set focus by setting this property (it is read-only). 此属性的典型用途是将其用作 SetterEventTrigger的依赖属性。The typical use for this property is to use it as a dependency property for a Setter or EventTrigger. 若要以编程方式设置焦点,请调用 FocusTo set focus programmatically, call Focus. 还可以通过用户操作或控件实现(可能包括鼠标捕获行为)来设置焦点。Focus can also be set by user action or by control implementations, which possibly include mouse capture behavior.

依赖项属性信息Dependency Property Information

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