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.
And for reference, I'm not applying a scaling transform (or any other sort of transform for that matter). Any help would be appreciated. :)
I think what you are looking for is to set this property in your XAML :
SnapsToDevicePixels="True"
Ref: Pixel Snapping in WPF Applications: .NET 3.5
The number of pixels that an element takes up will depend on several factors including:
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With