Link to home
Start Free TrialLog in
Avatar of Thomasian
ThomasianFlag for Philippines

asked on

Remove WPF TextBox Additional Padding

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
User generated image
ASKER CERTIFIED SOLUTION
Avatar of Carl Tawn
Carl Tawn
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Thomasian

ASKER

Is the -2 padding correct for all cases?
What about the padding of the stackpanel in which the controls are put?
StackPanel doesn't have a Padding property.
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.