美文网首页C# & WPF
[WPF] IInputElement Focus Issue,

[WPF] IInputElement Focus Issue,

作者: Kreiven | 来源:发表于2018-04-17 10:07 被阅读0次

    Keyboard Focus and Logical Focus

    Keyboard Focus

    Keyboard focus refers to the element that is currently receiving keyboard input. There can be only one element on the whole desktop that has keyboard focus. In WPF, the element that has keyboard focus will have IsKeyboardFocused set to true. The static property FocusedElement on the Keyboard class gets the element that currently has keyboard focus.

    In order for an element to obtain keyboard focus, the Focusable and the IsVisible properties on the base elements must be set to true. Some classes, such as the Panel base class, have Focusable set to false by default; therefore, you must set Focusable to true if you want such an element to be able to obtain keyboard focus.

    Keyboard focus can be obtained through user interaction with the UI, such as tabbing to an element or clicking the mouse on certain elements. Keyboard focus can also be obtained programmatically by using the Focus method on the Keyboard class. The Focus method attempts to give the specified element keyboard focus. The returned element is the element that has keyboard focus, which might be a different element than requested if either the old or new focus object block the request.

    Logical Focus

    Logical focus refers to the FocusManager.FocusedElement in a focus scope. A focus scope is an element that keeps track of the FocusedElement within its scope. When keyboard focus leaves a focus scope, the focused element will lose keyboard focus but will retain logical focus. When keyboard focus returns to the focus scope, the focused element will obtain keyboard focus. This allows for keyboard focus to be changed between multiple focus scopes but ensures that the focused element in the focus scope regains keyboard focus when focus returns to the focus scope.

    There can be multiple elements that have logical focus in an application, but there may only be one element that has logical focus in a particular focus scope.

    An element that has keyboard focus has logical focus for the focus scope it belongs to.

    Make TextBox(IInputElement) Focused

    public void FocusMethods()
    {
        //let's mock a mainwindow with a textbox in it.
        var element = (IInputElement)textbox;
        var window = (DependencyObject)mainwindow;
    
        //These methods are not compatible for unit test case.
        element.Focus();
        Keyboard.Focus(element);
        FocusManager.SetFocusedElement(window, element); //if window is active, then element is the 
    }
    

    Differences between IsKeyboardFocused and IsKeyboardFocusedWithIn

    Case:

    Now I am trying to add a trigger when user focus on the textbox control.

    If I add the trigger for TextEdit as follows, it dose not work, I mean IsKeyboardFocused value is always false, I think.
    Original TextBox, it is ok. Is there another property ?

       <!-- dxe:TextEdit Style-->
       <Style x:Key="DXTextEditStyle" TargetType="dxe:TextEdit">
            <Style.Triggers>
                <Trigger Property="IsKeyboardFocused" Value="true">
                    <Setter Property="Background" Value="Pink"/>
                </Trigger>
            </Style.Triggers>
        </Style>
    
    
    This is work,
        <!-- TextBox Style-->
        <Style x:Key="TextBoxStyle" TargetType="TextBox">
            <Style.Triggers>
                <Trigger Property="IsKeyboardFocused" Value="true">
                    <Setter Property="Background" Value="Pink"/>
                </Trigger>
            </Style.Triggers>
        </Style>
    
    

    Solution:

    Use IsKeyboardFocusedWithIn property instead.
    If you're trying to check whether a whole control, which contains a textbox, IsKeyboardFocused or not, you should use IsKeyboardFocusedWithIn.

    相关文章

      网友评论

        本文标题:[WPF] IInputElement Focus Issue,

        本文链接:https://www.haomeiwen.com/subject/hvkukftx.html