WPF drawing that stretches without stretching the Pen
geometry, stretch, wpf
Solution
I've done this by scaling a Path's Data, not the visual component.
- Place a Path in a Canvas.
- Set path.Data to a Geometry representing your data as percentages of the logical range.
- Set path.Data.Transform to a ScaleTransform with ScaleX and ScaleY bound to the actual width and height.
Problem
I need to draw some simple lines within a Border control (or similar) that always stretch to the bounds of the Border. Is there a way to stretch the lines only but not its pen? Without involving lots of C#? In this version the lines stretch: ``` <Border> <Border.Background> <DrawingBrush> <DrawingBrush.Drawing> <DrawingGroup> <GeometryDrawing Brush="Red"> <GeometryDrawing.Geometry> <GeometryGroup> <RectangleGeometry Rect="0,0 100,1000" /> <LineGeometry StartPoint="0,0" EndPoint="100,1000"/> <LineGeometry StartPoint="100,0" EndPoint="0,1000"/> </GeometryGroup> </GeometryDrawing.Geometry> <GeometryDrawing.Pen> <Pen Thickness="20" Brush="Black"/> </GeometryDrawing.Pen> </GeometryDrawing> </DrawingGroup> </DrawingBrush.Drawing> </DrawingBrush> </Border.Background> </Border> ``` The best solution I have come up with is this: ``` <Border> <Grid> <Path Stretch="Fill" Fill="Red" Stroke="Black" StrokeThickness="4" Data="M0,0 L100,0 100,1000 0,1000 z" /> <Path Stretch="Fill" Stroke="Black" StrokeThickness="4" Data="M 0,0 L0,0 100,1000" /> <Path Stretch="Fill" Stroke="Black" StrokeThickness="4" Data="M 100,0 L100,0 0,1000" /> </Grid> </Border> ``` But isn't there a better solution? That doesn't involve extra Grid?