Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Remove WPF TextBox Additional Padding

Posted on 2011-03-23
5
2,879 Views
Last Modified: 2013-12-17
How can I remove all padding/margin for a textbox control? I have already set margin and padding to zero but there's still space between controls.

XAML Code for the test app. I have included labels and textblocks controls in the test app to show what I mean.
<Window x:Class="Test"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Test" SizeToContent="WidthAndHeight">
    <StackPanel>
        <StackPanel Orientation="Horizontal">
            <TextBox Text="ABCD" Padding="0" Margin="0" BorderThickness="0" FontFamily="Arial" FontSize="20" />
            <TextBox Text="ABCD" Padding="0" Margin="0" BorderThickness="0" FontFamily="Arial" FontSize="20" />
            <TextBox Text="ABCD" Padding="0" Margin="0" BorderThickness="0" FontFamily="Arial" FontSize="20" />
        </StackPanel>
        <StackPanel Orientation="Horizontal">
            <Label Content="ABCD" Padding="0" Margin="0" BorderThickness="0" FontFamily="Arial" FontSize="20" />
            <Label Content="ABCD" Padding="0" Margin="0" BorderThickness="0" FontFamily="Arial" FontSize="20" />
            <Label Content="ABCD" Padding="0" Margin="0" BorderThickness="0" FontFamily="Arial" FontSize="20" />
        </StackPanel>
        <StackPanel Orientation="Horizontal">
            <TextBlock Text="ABCD" Padding="0" Margin="0" FontFamily="Arial" FontSize="20" />
            <TextBlock Text="ABCD" Padding="0" Margin="0"  FontFamily="Arial" FontSize="20" />
            <TextBlock Text="ABCD" Padding="0" Margin="0"  FontFamily="Arial" FontSize="20" />
        </StackPanel>
    </StackPanel>
</Window>

Open in new window

Result
Comparing TextBox, Label and TextBlock control
0
Comment
Question by:Thomasian
  • 2
  • 2
5 Comments
 
LVL 52

Accepted Solution

by:
Carl Tawn earned 500 total points
ID: 35198848
I think it is down the the way the textbox handles its content positioning. If you want to remove it you will need to force a negative padding to the left and right edges:
<TextBox Text="ABCD" Padding="-2,0" Margin="0" BorderThickness="0" FontFamily="Arial" FontSize="20" />

Open in new window

0
 
LVL 22

Author Comment

by:Thomasian
ID: 35203748
Is the -2 padding correct for all cases?
0
 
LVL 16

Expert Comment

by:CuteBug
ID: 35204717
What about the padding of the stackpanel in which the controls are put?
0
 
LVL 22

Author Comment

by:Thomasian
ID: 35204760
StackPanel doesn't have a Padding property.
0
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 35204874
The -2 should only be needed for TextBox elements. The TextBox has an implied internal pad to set it apart from label, etc. So you need to forcibly remove it with the negative padding.
0

Featured Post

Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

808 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question