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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T19:20:15+00:00 2026-05-28T19:20:15+00:00

I have written a simple WPF application in which I wanted to test the

  • 0

I have written a simple WPF application in which I wanted to test the Undo method provided by WPF TextBox. Basically on clicking the Undo button the value of TextBox has to be reverted.

This is my xaml code

<Window x:Class="WpfApplication2.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300">
  <StackPanel>
    <TextBox Name="textBox1"  />
    <Button Name="button1" Click="button1_Click">Undo</Button>
    <Button Name="button2" Click="button2_Click">Change value to 100</Button>
  </StackPanel>
</Window>

In my xaml.cs, I have written the following:

Case 1: Where Undo is working:: I click on “Change value to 100” button on the window, the TextBox’s text becomes 100. Now I click on Undo button the value of TextBox becomes empty.

public partial class Window1 : Window
{
    public Window1()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, RoutedEventArgs e)
    {
        textBox1.Undo();
    }

    private void button2_Click(object sender, RoutedEventArgs e)
    {
        textBox1.Text = "100";
    }
}

Case 2: Where Undo is not working:: I set the value of TextBox’s text to 100 in constructor, the TextBox’s text becomes 100. Now I click on Undo button the value of TextBox doesn’t become empty !!

public partial class Window1 : Window
{
    public Window1()
    {
        InitializeComponent();
        textBox1.Text = "100";
    }

    private void button1_Click(object sender, RoutedEventArgs e)
    {
        textBox1.Undo();
    }

    private void button2_Click(object sender, RoutedEventArgs e)
    {

    }
}

Why doesn’t the Undo functionality work in second case?
Is it because the TextBox is still not constructed ? But I checked all the properties of TextBox like Loaded, Initialized , etc which were all set to “True”

  • 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-28T19:20:16+00:00Added an answer on May 28, 2026 at 7:20 pm

    I’m not sure of the technical reason, but many controls in WPF won’t be fully functional until after the constructor. But you can still work around this by hooking the Loaded event.

    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();
            //textBox1.Text = "100";
            textBox1.Loaded += new RoutedEventHandler(textBox1_Loaded);
        }
    
        void textBox1_Loaded(object sender, RoutedEventArgs e)
        {
            textBox1.Text = "100";
        }
    
        private void button1_Click(object sender, RoutedEventArgs e)
        {
            textBox1.Undo();
        }
    
        private void button2_Click(object sender, RoutedEventArgs e)
        {
        }
    }
    

    The Undo button should work properly then.

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

Sidebar

Related Questions

I have written a simple feedback application in django. It's not particulairly complex, basically
I have written a simple test application using asp.net mvc with C#. The application
I have a simple WPF application I'm working on, which performs a SQL query
I have written a simple CRUD form which has one select list. However the
I have written a simple jQuery.ajax function which loads a user control from the
I have written the following simple test in trying to learn Castle Windsor's Fluent
I have written a simple application that displays a list of candidates for a
I have written a simple erlang app using gen_server. When starting it with application:start(myapp),
I'm using Console.WriteLine() from a very simple WPF test application, but when I execute
I have written a simple .NET webservice, which I will be hosted on 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.