Solved

Remove WPF TextBox Additional Padding

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
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…
This video discusses moving either the default database or any database to a new volume.

706 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

20 Experts available now in Live!

Get 1:1 Help Now