BorderThickness of 1 is 2 pixels thick - what am I missing here?

I have a Border in XAML / WPF that I use to underline the full paragraph for text headings in a dialog box. I set the BorderThickness property to "0,0,0,1". In some places, it ends with a 2-pixel underline, while in others it looks right like a single-pixel underline. What am I doing wrong?

Here is the control template that I use to replace the visual tree of my label (using the template is not essential, I would think):

<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 new to WPF, so I suspect I'm missing something fundamental in his rendering model.

  • Is displaying pixel border borders? Not like this, as I would have thought it would be partially transparent, if so.
  • Is there a way to ensure that I get what I ask for in terms of thickness?
  • Am I even mistaken howling?

And for reference, I do not use a scaling transformation (or any other transformation, for that matter). Any help would be greatly appreciated. :)

+6
wpf xaml rendering
source share
2 answers

The number of pixels an element occupies depends on several factors, including:

  • Screen DPI
  • Convert an element or any of its ancestors
  • Where is it displayed relative to the borders of the screen pixel

The numbers you use should be considered relative to each other, not absolute values ​​in terms of how they are displayed.

Having said that, WPF 4.0 will include Roundout Roundout , which can be used to reduce cases where lines that should be the same thickness are executed differently depending on whether they intersect with the border or not.

+6
source share

I think you want to set this property in your XAML:

SnapsToDevicePixels = "True"

Link: http://msdn.microsoft.com/en-us/library/vstudio/aa970908(v=vs.90).aspx

+7
source share

All Articles