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

  • SEARCH
  • Home
  • 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 7828681
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T10:26:26+00:00 2026-06-02T10:26:26+00:00

How can I clear my TextBox when it is focused? I want to do

  • 0

How can I clear my TextBox when it is focused? I want to do this in MVVM way. If it has meaning – my TextBox control has Text property binded with some property in ViewModel. TextBox displays sth like “50,30 zł”. It is uncomfortable for user to select text, delete it and write new text, so I want to clear old Text when Texbox is focused.

  • 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-06-02T10:26:28+00:00Added an answer on June 2, 2026 at 10:26 am

    You can write your own behavior or even control.
    I will try to explain the first one:

    First, you should a add reference to the System.Windows.Interactivity assembly.

    Then create a class (which will be the behavior) and derive it from System.Windows.Interactivity.Behavior< System.Windows.Controls.TextBox>, where templated (generic type) parameter is a control which should behave as I described.

    For example:

    class ClearOnFocusedBehavior : System.Windows.Interactivity.Behavior<System.Windows.Controls.TextBox>
    {
        private readonly RoutedEventHandler _onGotFocusHandler = (o, e) =>
                                                            {
                                                                ((System.Windows.Controls.TextBox) o).Text =
                                                                    string.Empty;
                                                            };
    
        protected override void OnAttached()
        {
            AssociatedObject.GotFocus += _onGotFocusHandler;
        }
    
        protected override void OnDetaching()
        {
            AssociatedObject.GotFocus -= _onGotFocusHandler;
        }
    }
    

    Next, put the following reference declaration in your parent window in xaml

    <Window x:Class="ManagementSolution.Views.UpdatePersonsWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity">
        //namespace with ur behaviors
        xmlns:behaviors="clr-namespace:ManagementSolution.Helper.Behaviours"
        //...
    </Window>
    

    Finally add the behavior to the appropriate UI element(TextBox in our case):

    <TextBox x:Name="PersonFirstNameTextBox"
                 Grid.Column="1"
                 Margin="5,0"
                 HorizontalAlignment="Stretch"
                 VerticalAlignment="Center"
                 Style="{StaticResource TextBoxValidationStyle}"
                 TextWrapping="Wrap"
                 d:LayoutOverrides="Width, Height">
            //behavior added as the content
            <i:Interaction.Behaviors>   
                <behaviors:ClearOnFocusedBehavior /> 
            </i:Interaction.Behaviors>
            <TextBox.Text>
                <Binding Path="PersonFirstName"
                         UpdateSourceTrigger="PropertyChanged"
                         ValidatesOnDataErrors="True">
                    <!--
                        <Binding.ValidationRules>
                        <rules:SingleWordNameValidationRule />
                        </Binding.ValidationRules>
                    -->
                </Binding>
            </TextBox.Text>
        </TextBox>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hoping someone can clear this up for me. Let's say I have 2 globals:
I hope I can make myself clear in English and in what I want
I can't see a clear mistake in this code. Instead of validating my fields,
in this picture,I can see: MenuStrip ,??, TextBox how do I an bar like
I have an ASP.NET user control that contains a textbox which has an AJAX
I am using this JS to clear text box when user click on it,
I have an asp.net form . In this form I want to clear all
I have a textbox that has a piece of text in it already so
can you set the the image size of this control with css? <img style=border-width:
I need to bind a TextBox's Text to a property of a class that

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.