Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6646025
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T00:21:46+00:00 2026-05-26T00:21:46+00:00

I have a logic with my textbox which says, on focus move the selection

  • 0

I have a logic with my textbox which says, on focus move the selection start to the last character, so that the editing person can just continue writing.

This worked perfectly with this:

    private void TextBox_GotFocus(object sender, EventArgs e)
    {
        var textBox = sender as TextBox;
        if (textBox == null) return;

        textBox.SelectionStart = textBox.Text.Length;
    }

and

    <Style TargetType="{x:Type TextBox}">
        <EventSetter Event="GotFocus" Handler="TextBox_GotFocus"/>
    </Style>

and

<DataGridTemplateColumn.CellEditingTemplate>
    <DataTemplate>
        <TextBox Name="SomeTextBox" Text="{Binding Path=Pressure, UpdateSourceTrigger=PropertyChanged}" Padding="2,0,0,0" />
        <DataTemplate.Triggers>
            <Trigger SourceName="SomeTextBox" Property="IsVisible" Value="True">
                <Setter TargetName="SomeTextBox" Property="FocusManager.FocusedElement" Value="{Binding ElementName=SomeTextBox}"/>
            </Trigger>
        </DataTemplate.Triggers>
    </DataTemplate>
</DataGridTemplateColumn.CellEditingTemplate>

but when I moved this to:

<DataGridTemplateColumn.CellEditingTemplate>
    <DataTemplate>
        <ContentControl Content="{Binding Path=Pressure, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" ContentTemplate="{StaticResource DataGridTextBoxEdit}" />
    </DataTemplate>
</DataGridTemplateColumn.CellEditingTemplate>

and a reusable template:

    <DataTemplate x:Key="DataGridTextBoxEdit">
        <TextBox Name="TextBox" Text="{Binding Content, RelativeSource={RelativeSource AncestorType=ContentControl}}" Padding="2,0,0,0" />
        <DataTemplate.Triggers>
            <Trigger SourceName="TextBox" Property="IsVisible" Value="True">
                <Setter TargetName="TextBox" Property="FocusManager.FocusedElement" Value="{Binding ElementName=TextBox}"/>
            </Trigger>
        </DataTemplate.Triggers>
    </DataTemplate>

it just stopped working. The GotFocus event fires off, however I simply cannot assign anything to SelectionStart, it just doesn’t save it. Tried even hardcoding:

    private void TextBox_GotFocus(object sender, EventArgs e)
    {
        var textBox = sender as TextBox;
        if (textBox == null) return;

        textBox.SelectionStart = 5;
    }

but didn’t work. It’s also worth to note that Text is empty, only DataContext is filled at this point, however as the SelectionStart is not taking anything (saving), it’s no good to me.

What am I doing wrong?

Kind regards,
Vladan

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T00:21:47+00:00Added an answer on May 26, 2026 at 12:21 am

    At the point where the TextBox gets focus it does not have any text yet, this means the handler fires before the DataGrid assigns the value. One way to get arround this is to check for the first text change and then do the selection change, e.g.

    private void TextBox_GotFocus(object sender, EventArgs e)
    {
        var textBox = sender as TextBox;
        if (textBox == null) return;
    
        var desc = DependencyPropertyDescriptor.FromProperty(TextBox.TextProperty, typeof(TextBox));
        EventHandler handler = null;
        handler = new EventHandler((s, _) =>
            {
                desc.RemoveValueChanged(textBox, handler);
                textBox.SelectionStart = textBox.Text.Length;
            });
        desc.AddValueChanged(textBox, handler);
    }
    

    (This code may not be very clean, use at own risk)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm creating a login page. I want to create ASP.NET TextBox controls that have
I'm sure you've all seen them. Line of Business UIs that have logic such
I have some logic, which defines and uses some user-defined types, like these: class
I have authentication logic that uses cookies. The model attributes are initialized from cookies,
I have some logic in a method that operates on a specified type and
I have some logic that depends upon two properties being set, as it executes
I recently inherited a VBA macro that needs to have validation logic added to
I have a business logic classes that are written in pure C# (without any
I have some custom logic that needs to be executed every single time a
I have a numeric textbox in C# .NET which is nothing more than a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.