none
textBox padding

    General discussion

  • Even if I re-define the TextBox template, I see padding between borders and text.

    - What causes the padding?

    - How do I get rid of the padding?

    Thank you.

    • Changed type Jim Zhou - MSFT Friday, June 26, 2009 6:39 AM customer does not provide feedback
    Wednesday, June 17, 2009 10:16 AM

All replies

  • Hi,

    Could you please post the template you created for the TextBox here?

    Thanks.


    Jim Zhou -MSFT
    Tuesday, June 23, 2009 1:57 PM
  • We are changing the issue type to “Comment” because you have not followed up with the necessary information. If you have more time to look at the issue and provide more information, please feel free to change the issue type back to "Question” by clicking the "Options" link at the top of your post, and selecting "Change Type" menu item from the pop menu. If the issue is resolved, we will appreciate it if you can share the solution so that the answer can be found and used by other community members having similar questions.

     

    Thank you!


    Jim Zhou -MSFT
    Friday, June 26, 2009 6:38 AM
  • <Style x:Key="{x:Type TextBox}" TargetType="{x:Type TextBoxBase}">
      <Setter Property="SnapsToDevicePixels" Value="True"/>
      <Setter Property="OverridesDefaultStyle" Value="True"/>
      <Setter Property="KeyboardNavigation.TabNavigation" Value="None"/>
      <Setter Property="FocusVisualStyle" Value="{x:Null}"/>
      <Setter Property="Background" Value="#ffffff" />
      <Setter Property="BorderBrush" Value="#000000" />
      <Setter Property="BorderThickness" Value="1" />
      <Setter Property="AllowDrop" Value="true"/>
      <Setter Property="Template">
        <Setter.Value>
          <ControlTemplate TargetType="{x:Type TextBoxBase}">
            <Border 
              Name="Border"
              CornerRadius="0" 
              Padding="0"
              Background="{TemplateBinding Background}"
              BorderBrush="{TemplateBinding BorderBrush}"
              BorderThickness="{TemplateBinding BorderThickness}" 
            >
              <ScrollViewer 
                x:Name="PART_ContentHost" 
                Margin="0" 
                Padding="0"
              />
            </Border>
            
            <ControlTemplate.Triggers>
              <Trigger Property="IsEnabled" Value="False">
                <Setter TargetName="Border" Property="Background" Value="{StaticResource DisabledBackgroundBrush}"/>
                <Setter TargetName="Border" Property="BorderBrush" Value="{StaticResource DisabledBackgroundBrush}"/>
                <Setter Property="Foreground" Value="{StaticResource DisabledForegroundBrush}"/>
              </Trigger>
            </ControlTemplate.Triggers>
          </ControlTemplate>
        </Setter.Value>
      </Setter>
    </Style>
    

    Notes:

    1. On my machine, I see extra white space between the left border and contained text, i.e. text is not flush with the border.

    2. On my machine, setting the left margin of the ScrollViewer to negative 3 or 4, I can get text to be flush with the left border.

    Where's the magic number coming from?

    Tuesday, June 30, 2009 10:39 PM
  • Hi,

     

    --> On my machine, I see extra white space between the left border and contained text, i.e. text is not flush with the border.

     

    I ran the code you provided above, but can not got the effect you mentioned. Is there any other styles you set for the TextBox?

     

    Thanks.


    Jim Zhou -MSFT
    Wednesday, July 01, 2009 7:00 AM
  • >> Is there any other styles you set for the TextBox?

    Ah, you got me there.  In my project, I had additional style that defined the font family and size for the entire window.  After I removed it, the gap in the TextBox narrowed.

    Hmm, quite bizarre. And annoying, too.  I will not be able to set font globally.

    >> can not got the effect you mentioned.

    All isn't hunky-dory.  If you run my bare-bone program in the attachment, you should see a difference.  However, it isn't as bad as before.

    <Window
      x:Class="TextBoxMsdnForum01Wpf.Window1"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      Title="Window1"
      Width="300" Height="300"
      ResizeMode="CanResizeWithGrip"
      WindowStartupLocation="CenterScreen"
    >
      <Window.Resources>
    
        <Style x:Key="{x:Type TextBox}" TargetType="{x:Type TextBoxBase}">
          <Setter Property="SnapsToDevicePixels" Value="True"/>
          <Setter Property="OverridesDefaultStyle" Value="True"/>
          <Setter Property="KeyboardNavigation.TabNavigation" Value="None"/>
          <Setter Property="FocusVisualStyle" Value="{x:Null}"/>
          <Setter Property="Background" Value="#ffffff" />
          <Setter Property="BorderBrush" Value="#000000" />
          <Setter Property="BorderThickness" Value="1" />
          <Setter Property="AllowDrop" Value="true"/>
          <Setter Property="Template">
            <Setter.Value>
              <ControlTemplate TargetType="{x:Type TextBoxBase}">
                <Border 
                  Name="Border"
                  CornerRadius="0" 
                  Padding="0"
                  Background="{TemplateBinding Background}"
                  BorderBrush="{TemplateBinding BorderBrush}"
                  BorderThickness="{TemplateBinding BorderThickness}" 
                >
                  <ScrollViewer 
                    x:Name="PART_ContentHost" 
                    Margin="0" 
                    Padding="0"
                  />
                </Border>
    
              </ControlTemplate>
            </Setter.Value>
          </Setter>
        </Style>
      </Window.Resources>
      
      <StackPanel
        Margin="5"
      >
        <TextBox
          Text="This is a TextBox"
          FontFamily="verdana"
          FontSize="12pt"
          FontWeight="Normal"
          Foreground="#000000"
        />
        <Border
          BorderBrush="#000000"
          BorderThickness="1"
        >
          <TextBlock 
            Text="This is a TextBlock"
            FontFamily="verdana"
            FontSize="12pt"
            FontWeight="Normal"
            Foreground="#000000"
          />
        </Border>
      </StackPanel>
    </Window>
    
    • Edited by foobar101 Thursday, July 02, 2009 12:25 PM
    Wednesday, July 01, 2009 8:42 AM
  • Hi,

     

    It seems the gap becomes smaller after I remove the FontFamily for TextBox. The code like the following:

    <TextBox

              Text="This is a TextBox"

              FontSize="20"

              FontWeight="Normal"

              Foreground="#000000"/>

     

    Thanks.


    Jim Zhou -MSFT
    Thursday, July 02, 2009 6:36 AM
  • >> the gap becomes smaller after I remove the FontFamily

    Perhaps.  I'm not sure.

    The point is the TextBox includes phantom padding three-pixel thick by my unscientific measurement.  I've discovered this when I tried unsuccessfully to set the width of a TextBox at runtime by computing the width of contained text, using the FormattedText trick.

    The phantom three-pixel padding seems to be a bug or a built-in feature like the infamous six-pixel _defalut bug in WPF (look for "_defalut" in the weblog of Ian Griffiths if you don't know what it is).

    Jim, can you investigate or get anybody of authority at Microsoft to tell us

    - What causes the phantom padding?
    - Exactly how thick the phantom padding is?
    - If it is a feature, where is it documented?
    - If it is a bug, will it be fixed in .Net Framework 4.0?

     

    Thursday, July 02, 2009 12:30 PM
  • Hi, I am replying very late on this forum but as there is no solution mentioned till now so thought it will be worth commenting.

    In the above style you have given padding/margin as zero in template but still your text is not starting from flush left, this is because inside the ScrollViewer(PART_ContentHost) we have ScrollContentPresenter and TextBoxView which are created internally , for the TextBoxView there is a default Margin="2,0,2,0" so that is why text starts with default 2px padding from left side. So the phantom padding is 2px, if you give 2px negative left padding then your text should start from flush left. No need to give margin/Padding inside template ,just give <Setter Property="Padding" Value="-2,0,0,0"/> as a setter in style.

    Wednesday, July 13, 2011 7:20 AM