Cursor Focus on Textbox in WPF/C#

前端 未结 8 985
星月不相逢
星月不相逢 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:59

    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}" />
    
    0 讨论(0)
  • 2020-12-23 23:02

    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;
    }
    
    0 讨论(0)
提交回复
热议问题