R – BorderThickness of 1 renders with a 2-pixel thickness – what am I missing here


I've got a Border in XAML/WPF that I'm using to give a full-paragraph-width underline to text headings in a dialog. I set its BorderThickness property to "0,0,0,1". In some places, it ends up being rendered with a 2-pixel thick underline while in others it appears correctly as a single-pixel underline. What am I doing wrong?

Here's the control template I'm using to replace my label's visual tree (the use of a template is inconsequential, I would've thought):

<ControlTemplate x:Key="HeaderTemplate" TargetType="{x:Type Label}">
<Border BorderThickness="0,0,0,1" Margin="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Margin}">
    <LinearGradientBrush StartPoint="0,0" EndPoint="1,0">
      <GradientStop Offset="0" Color="Black"/>
      <GradientStop Offset="0.6" Color="Black"/>
      <GradientStop Offset="1" Color="Transparent"/>
  <TextBlock Text="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Content}" Style="{StaticResource HeaderStyle}"
             Margin="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Padding}"/>

I'm pretty new to WPF, so I suspect I'm missing something fundamental about its rendering model.

  • Is the border rendering over a pixel boundary? Doesn't seem that way, as I would've thought it would be partially transparent if so.
  • Is there a way to guarantee that I get what I'm asking for in terms of thickness?
  • Have I even made a howling error?

And for reference, I'm not applying a scaling transform (or any other sort of transform for that matter). Any help would be appreciated. 🙂

Best Solution

The number of pixels that an element takes up will depend on several factors including:

  • The DPI of the screen
  • Transformations on the element or any of its ancestors
  • Where it is rendered in relation to the pixel boundaries of the screen

The numbers you're using should be considered to be relative to each other rather than absolute values in terms of how they are rendered.

Having said that, WPF 4.0 will include Layout Rounding that you can use to reduce cases where lines that are supposed to be the same thickness are rendered differently depending on whether or not they happen to cross a pixel boundary.

Related Question