Thursday 18 February 2010

Remove padding from TextBox in WPF

If you add the attribute

Padding="0, 0, 0, 0"

to most WPF elements e.g. <Label></Label>, the text is placed in the top left hand corner with no space between the left and top sides.  However if you try it with a TextBox element it appears to have no effect.

Fortunately as a workaround you can set the padding to have negative values.  So e.g. if I want a standard TextBox element, with standard fontsize, with no space above the text, doing something like:

Padding="0, -2, 0, 0"

Will have the desired effect.

3 comments:

  1. Hello, and thanks for posting on this subject. This does not appear to work for wpf textboxes in .net 4.0.

    I am trying to do this and the designer tells me:
    '0,-2,0,0' is not a valid value for property 'Padding'

    Any other ideas?

    ReplyDelete
  2. It works for me. I did get that message when I was using a textblock, but it works with textboxes.

    ReplyDelete
  3. '0,-2,0,0' is not a valid value for property 'Padding'
    Ignore Anonymous above who says it worked.

    ReplyDelete