Cursor Focus on Textbox in WPF/C#

前端 未结 8 984
星月不相逢
星月不相逢 2020-12-23 22:14

I am currently in the process of creating a Onscreen keyboard. I am handling the button click using routedcommands. The issue is that when i click on the button in keyboard

相关标签:
8条回答
  • 2020-12-23 22:40

    Your problem can be solved by using a separate focus scope for your "keyboard". Just apply the following property to the control that contains all of your buttons and then they will be in a separate focus scope and will not have the focus set to them when clicked

    FocusManager.IsFocusScope="True"
    
    0 讨论(0)
  • 2020-12-23 22:41

    Get the reference for the specific control(in that case TextBox). After click, in Button_Click method paste this:

    Dispatcher.BeginInvoke((ThreadStart)delegate
                {
                    control.Focus();
                });
    
    0 讨论(0)
  • 2020-12-23 22:43

    The way I solved this problem was to set focusable=false to all the buttons/controls on the keyboard. That way you don't lose focused of the current control.

    0 讨论(0)
  • 2020-12-23 22:47
    Textbox.Focus();
    

    this will focus on the textbox

    0 讨论(0)
  • 2020-12-23 22:57

    I had to use this to get my desired result

    FocusManager.SetFocusedElement(this, UserNameautoCompleteBox);
    
    Key key = Key.Enter;                    // Key to send
    var target = Keyboard.FocusedElement;    // Target element
    RoutedEvent routedEvent = Keyboard.KeyDownEvent; // Event to send
    
    target.RaiseEvent(
        new KeyEventArgs(
            Keyboard.PrimaryDevice,
            PresentationSource.FromVisual(UserNameautoCompleteBox),
            0,
            key) { RoutedEvent = routedEvent }
    );
    
    0 讨论(0)
  • 2020-12-23 22:58

    To set logical focus to an input control

    FocusManager.SetFocusedElement(this, textboxJack);     // set logical focus
    

    To set keyboard focus to an input control

    Keyboard.Focus(textboxJill);                             // set keyboard focus
    

    To know the difference between logical and keyboard focus

    Input Overview - Focus on MSDN

    0 讨论(0)
提交回复
热议问题