deepak
deepak

Reputation:

Cursor Focus on Textbox in WPF/C#

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 panel the focus shifts to the button rather than on the Textbox. The requirement states that a cursor should always appear in the text box to indicate the position where the next character will be inserted. Is there a way i can keep focus on the textbox while button is clicked.

Upvotes: 22

Views: 82727

Answers (8)

Anya Hope
Anya Hope

Reputation: 1361

I wanted to do the same thing, but nothing seemed to work for me. I really needed an answer that worked purely in XAML as I'm working with MVVM.

I finally found this example: http://spin.atomicobject.com/2013/03/06/xaml-wpf-textbox-focus/

and modified it to this:

In the 'Resources' section:

    <Style x:Key="FocusTextBox" TargetType="Grid">
        <Style.Triggers>
            <DataTrigger Binding="{Binding ElementName=textBoxName, Path=IsVisible}" Value="True">
                <Setter Property="FocusManager.FocusedElement" Value="{Binding ElementName=textBoxName}"/>
            </DataTrigger>
        </Style.Triggers>
    </Style>

In my grid definition:

<Grid Style="{StaticResource FocusTextBox}" />

Upvotes: 1

Brendan Botond
Brendan Botond

Reputation: 11

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 }
);

Upvotes: 1

mashet
mashet

Reputation: 876

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();
            });

Upvotes: 13

Jonas Stawski
Jonas Stawski

Reputation: 6752

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.

Upvotes: 3

gokul
gokul

Reputation: 305

Textbox.Focus();

this will focus on the textbox

Upvotes: 3

Xiaosu
Xiaosu

Reputation: 615

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"

Upvotes: 2

Josh Fischer
Josh Fischer

Reputation: 432

I like these do-my-homework-for-me questions; "the requirement states"...priceless. For those who find this via Google, the trick to progmatically moving the cursor in a WPF TextBox is to use the SelectioNStart property.

private void Button_Click(object sender, RoutedEventArgs e)
{
    textBox.Focus();
    textBox.SelectionStart = textName.Text.Length;
}

Upvotes: 8

Gishu
Gishu

Reputation: 136613

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

Upvotes: 37

Related Questions