Click here to Skip to main content
14,036,509 members
Click here to Skip to main content
Add your own
alternative version


1 bookmarked
Posted 4 Apr 2011
Licenced CPOL

TextBox.TextChanged Event Fires Twice on the Phone

, 4 Apr 2011
Rate this:
Please Sign up or sign in to vote.
A fix to the TextBox TextChanged event firing twice on the Windows Phone 7 emulator

It's very rare that I ever user the TextChanged event directly… but never say never!

I recently needed it and for some weird reason, it fired twice for every key pressed. Here is a simplified example of the problem. I added a simple TextBlock and TextBox:









    mc:Ignorable="d" d:DesignWidth="480" d:DesignHeight="768"

    FontFamily="{StaticResource PhoneFontFamilyNormal}"

    FontSize="{StaticResource PhoneFontSizeNormal}"

    Foreground="{StaticResource PhoneForegroundBrush}"

    SupportedOrientations="Portrait" Orientation="Portrait"


        <TextBlock Text="Count: 0" x:Name="counterTextBlock" />
        <TextBox x:Name="textBox" TextChanged="textBox_TextChanged" />

And then attached the TextChanged event handler from XAML. The event handler just increments a counter and prints it on the screen:

public partial class MainPage : PhoneApplicationPage
    public MainPage()

    int count = 0;

    private void textBox_TextChanged(object sender, TextChangedEventArgs e)
        counterTextBlock.Text = "Count: " + count;

Running it, for every letter I type, it increments twice (sounds weird, but try it).

After searching a bit, I found this post on Stackoverflow (by none other than the legend Jon Skeet) about exactly the same problem. It turns out that the problem is how the TextBox was styled to Metro-fy™ it… Here is a simplified version of the original template:

<Style x:Key="SimplefiedTextBoxStyle" TargetType="TextBox">
    <Setter Property="Template">
            <ControlTemplate TargetType="TextBox">
                <Grid Background="Transparent">
                    <Border x:Name="EnabledBorder">
                        <ContentControl x:Name="ContentElement" />
                    <Border x:Name="DisabledOrReadonlyBorder" 

                        <TextBox x:Name="DisabledOrReadonlyContent" 

                           Text="{TemplateBinding Text}" />

Notice the second TextBox, if you remove it… the event only fires once (but then you have no differentiation when disabled/read only).


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

South Africa South Africa
No Biography provided

You may also be interested in...

Comments and Discussions

-- There are no messages in this forum --
Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web02 | 2.8.190424.1 | Last Updated 4 Apr 2011
Article Copyright 2011 by rudigrobler
Everything else Copyright © CodeProject, 1999-2019
Layout: fixed | fluid