Solved

Remove WPF TextBox Additional Padding

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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

For most people, the WrapPanel seems like a magic when they switch from WinForms to WPF. Most of us will think that the code that is used to write a control like that would be difficult. However, most of the work is done by the WPF engine, and the W…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
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…
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…

815 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

11 Experts available now in Live!

Get 1:1 Help Now