Solved

Remove WPF TextBox Additional Padding

Posted on 2011-03-23
5
2,822 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
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…
As a trusted technology advisor to your customers you are likely getting the daily question of, ‘should I put this in the cloud?’ As customer demands for cloud services increases, companies will see a shift from traditional buying patterns to new…

911 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now