Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF TextBox ugly borders problem

I want to override the default TextBox borders in WPF. I have this style that applies on all TextBoxes.

<!-- StyleTextBox-->
<Style x:Key="StyleTextBox" TargetType="{x:Type TextBox}">
    <Setter Property="MinHeight" Value="20" />
    <Setter Property="HorizontalAlignment" Value="Left"/>
    <Setter Property="Margin" Value="3"/>
    <Setter Property="IsEnabled" Value="{DynamicResource WriteAble}"/>
    <Setter Property="SnapsToDevicePixels" Value="True" />
    <Setter Property="VerticalContentAlignment" Value="Top" />
    <Setter Property="BorderThickness" Value="1" />
    <Setter Property="BorderBrush" Value="{StaticResource ButtonFont_DarkGray}" />
    <Style.Triggers>
        <!--Resolves multiline textbox vertical alignment problem-->
        <Trigger Property="TextWrapping" Value="NoWrap">
            <Setter Property="VerticalContentAlignment" Value="Center" />
        </Trigger>
    </Style.Triggers>
</Style> 

I added SnapsToDevicePixels="True" to display borders correctly on LCD monitors.

But, every TextBox seems to be different. Some borders are missing, or gray.. Does anyone know why?

like image 284
theSpyCry Avatar asked Jul 14 '09 07:07

theSpyCry


2 Answers

You could try editing the template for the textboxes and changing the border name Bd to a "real" border instead of the chrome one. Like this:

<ControlTemplate x:Key="TextBoxBaseControlTemplate1" 
          TargetType="{x:Type TextBoxBase}">
  <Border x:Name="Bd" SnapsToDevicePixels="True" 
          Background="{TemplateBinding Background}" 
          BorderBrush="{TemplateBinding BorderBrush}"
          BorderThickness="{TemplateBinding BorderThickness}" >
    <ScrollViewer x:Name="PART_ContentHost" 
              SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
  </Border>
  <ControlTemplate.Triggers>
    <Trigger Property="IsEnabled" Value="False">
      <Setter Property="Background" TargetName="Bd" 
            Value="{DynamicResource {x:Static SystemColors.ControlBrushKey}}"/>
      <Setter Property="Foreground" 
           Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/>
    </Trigger>
  </ControlTemplate.Triggers>
</ControlTemplate>

add this setter to your style to enable the template:

<Setter Property="Template" 
        Value="{DynamicResource TextBoxBaseControlTemplate1}"/>  
like image 200
Sorskoot Avatar answered Oct 26 '22 23:10

Sorskoot


WPF tries to be device independent when rendering UI to the monitor, and won't draw things "pixel perfect" unless you tell it to. Try adding this to your style:

<Setter Property="SnapsToDevicePixels" Value="True" />

That should tell WPF to render each 1-pixel-thick border along a single pixel line.

like image 41
Matt Hamilton Avatar answered Oct 26 '22 23:10

Matt Hamilton