Matt
Matt

Reputation: 22103

How can I remove the margins around text in a WPF label?

I am trying to make a little virtual keyboard out of labels. The following is my keyboard in XAML (but with more than just 3 keys):

<StackPanel Orientation="Vertical">
    <StackPanel Orientation="Horizontal" HorizontalAlignment="Center">
        <Border BorderThickness="1" BorderBrush="DarkGray">
            <Label Content="A" FontSize="12" MouseDown="KeyButton_Click" />
        </Border>
        <Border BorderThickness="1" BorderBrush="DarkGray">
            <Label Content="B" FontSize="12" MouseDown="KeyButton_Click" />
        </Border>
    </StackPanel>
    <StackPanel Orientation="Horizontal" HorizontalAlignment="Center">
        <Border BorderThickness="1" BorderBrush="DarkGray">
            <Label Content="C" FontSize="12" MouseDown="KeyButton_Click" />
        </Border>
    </StackPanel>
</StackPanel>

The problem with this is that there is too much space surrounding the text in the labels, causing the keyboard to be much bigger than it needs to be. If I manually set the height and width of the labels, that will (1) not account for differences in fonts and (2) will cut of part of the letter rather than the top and left margins. Is there any other way to shrink these margins to be just about the same size as the text itself?

Upvotes: 22

Views: 18847

Answers (2)

Shaggydog
Shaggydog

Reputation: 3788

Set padding to 0.

I had the same problem. Upon examining the properties of a label in the properties window, I discovered the default padding of a label is 5. Setting it to 0 did the trick.

Upvotes: 74

Harry
Harry

Reputation: 1705

Use TextBlock instead of Label

Upvotes: 15

Related Questions