Click here to Skip to main content
13,044,115 members (82,998 online)
Click here to Skip to main content
Add your own
alternative version


1 bookmarked
Posted 25 Dec 2012

Windows Phone 7 TextBox style margin issue

, 19 Mar 2013
Rate this:
Please Sign up or sign in to vote.
Avoid unwanted margin in disabled TextBox.


I found a strange behavior of TextBox. When it's in disabled state and it has a non-zero margin, the inner text looks shifted:

To reproduce it, just create a new Silverlight WP app and add the following code to a grid named ContentPanel:

<StackPanel Margin="0,12,0,0">
		<Style TargetType="TextBlock">
			<Setter Property="Margin" Value="25,0,25,0"/>
		<Style TargetType="TextBox">
			<Setter Property="Margin" Value="33,0,13,0"/>

	<TextBlock Text="Name:" />
	<TextBox Text="some test text" IsEnabled="False" />

	<TextBlock Text="E-mail:"/>
	<TextBox Text="" InputScope="EmailNameOrAddress" />

The reason is that in the default control template disabled state is described using TextBox. Here is a short version of the default control template (you can get a full version with Expression Blend):  

<Border x:Name="EnabledBorder" ... > 
    <ContentControl x:Name="ContentElement" .../> 
<Border x:Name="DisabledOrReadonlyBorder" ... > 
    <TextBox .../> 

In the case of setting IsEnabled="False", EnabledBorder is hidden and DisabledOrReadonlyBorder is visible. So the textbox is rendered using TextBox.  

You will notice in the first code sample the style for TextBox without x:Key. It means, that style applied to any of the child TextBox. So the margin from style's setter is applied to the control template’s inner TextBox too. And that’s the reason why we have a shifted text in the disabled state!  

As a solution 

You can add x:Key for TextBox style and apply it manually to every TextBox. In this case the disabled state will work normally.

The same is valid for PasswordBox too. 


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


About the Author

Russian Federation Russian Federation
This member doesn't quite have enough reputation to be able to display their biography and homepage.

You may also be interested in...

Comments and Discussions

-- There are no messages in this forum --
Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.170713.1 | Last Updated 19 Mar 2013
Article Copyright 2012 by VitusD
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid