Increase StrokeThickness but maintain dimensions o

2019-01-24 17:19发布

问题:

I have a Path that normally has a StrokeThickness of 1. Under certain circumstances, it needs to have a StrokeThickness of 10. When I increase the stroke thickness, I don't want the path to take any additional space.

By default, just increasing the StrokeThickness increases the rendered size of the path. So you get something like this (the blue outline extends beyond the black boundary):

This is what I'm trying to achieve (the blue outline stays within the black boundary):

I can think of two mathematical ways to compensate for the increased StrokeWidth:

  1. Manually adjust the points of the triangle inward.
  2. Use a ScaleTransform on the Geometry of the Path.

Both of these would be somewhat problematic/complex. Is there an easier way?

回答1:

You could clip the path by its own geometry like this:

<Path ... Clip="{Binding Data, RelativeSource={RelativeSource Self}}"/>

but then you would need to double the StrokeThickness, since only half of the stroke is visible.



回答2:

On a whim I set StrokeThickness = -1 on my Rectangle and it did exactly what I wanted it to: the stroke goes on the inside of the Rectangle rather than on the outside.