views:

117

answers:

1

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}">
  <Border.BorderBrush>
    <LinearGradientBrush StartPoint="0,0" EndPoint="1,0">
      <GradientStop Offset="0" Color="Black"/>
      <GradientStop Offset="0.6" Color="Black"/>
      <GradientStop Offset="1" Color="Transparent"/>
    </LinearGradientBrush>
  </Border.BorderBrush>
  <TextBlock Text="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Content}" Style="{StaticResource HeaderStyle}"
             Margin="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Padding}"/>
</Border>
</ControlTemplate>

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. :)

+3  A: 

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.

GraemeF
So what we're saying is, for the time being, I have to let go of my desire to control things as strictly as I can with, say, HTML and CSS? :)
Mal Ross
In a nutshell, yes :)
GraemeF
I've been working on a somewhat complex control for a couple days and it continually frustrates me by smearing border colors, etc. The first question I look at has the answer and, while my obsessive nature tells me there MUST be a work-around, I appreciate the insight. Thank you!
Brad
Actually, merely setting SnapsToDevicePixels on the Border seems to have achieved what I wanted. My underlines are now all 1 pixel thick and nice and crisp.
Mal Ross