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 378465
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T14:47:45+00:00 2026-05-12T14:47:45+00:00

What I want is simply that all my TextBoxes as by default set their

  • 0

What I want is simply that all my TextBoxes as by default set their cursor at the end of the text so I want in pseudocode:

if (TextChanged) textbox.SelectionStart = textbox.Text.Length;

Because I want that all textboxes in my application are affected I want to use a style for that. This one won’t work (for obvious reasons) but you get the idea:

<Style TargetType="{x:Type TextBox}">
  <Style.Triggers>
    <EventTrigger RoutedEvent="TextChanged">
      <EventTrigger.Actions>
        <Setter Property="SelectionStart" Value="{Binding Text.Length}"/>
      </EventTrigger.Actions>
    </EventTrigger>
  </Style.Triggers>
</Style>

EDIT:
One important thing is that the SelectionStart property should only be set if the Text property was assigned programmatically, not when the user edits the textbox.

  • 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-12T14:47:45+00:00Added an answer on May 12, 2026 at 2:47 pm

    Are you sure you want this behavior for ALL textboxes in your app ? it will make it almost impossible (or at least very painful) for the user to edit text in the middle of the TextBox…

    Anyway, there is a way to do what you want… Assuming your style is defined in a ResourceDictionary file. First, you need to create a code-behind file for the resource dictionary (for instance, Dictionary1.xaml.cs). Write the following code in this file :

    using System.Windows.Controls;
    using System.Windows;
    
    namespace WpfApplication1
    {
        partial class Dictionary1
        {
            void TextBox_TextChanged(object sender, RoutedEventArgs e)
            {
                TextBox textBox = sender as TextBox;
                if (textBox != null)
                    textBox.SelectionStart = textBox.Text.Length;
            }
        }
    }
    

    In the XAML, add the x:Class attribute to the ResourceDictionary element :

    <ResourceDictionary x:Class="WpfApplication1.Dictionary1"
                        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    

    Define your style as follows :

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

    The TextBox_TextChanged method will now handle the TextChanged event of all textboxes.

    It should be possible to write the code inline in the XAML using the x:Code attribute, but I couldn’t make it work…

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

Sidebar

Related Questions

I learning Perl and I want to create a simple application that gets all
I have several sql queries that I simply want to fire at the database.
I simply want to generate a class with attributes that comes from a Database
I simply want to show off a model in a table that is paginated
I want to simply read some JSON data from a URL, then turn that
Here's my list that I want to expand/collapse: My goal is simply to toggle
I want to find all the required fields on my form that are within
I want to make a simple program that sends random keystrokes to my other
I want to implement a simple link that when clicked, deletes the item from
I want to write a simple program that takes a moderately sized wav file

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.