#1,145 – Using RenderSize in Custom Shape

When drawing a geometry in a custom Shape element, you could draw using hard-coded coordinates.  It’s more common, however, to use the RenderSize property of the UIElement to render the object so that the geometry scales based on the size of the control.

Below, we create a custom shape that draws a diagonal line from the upper left corner of the control to the lower right.

    public class MyShape : Shape
        protected override Geometry DefiningGeometry
                double maxWidth = RenderSize.Width;
                double maxHeight = RenderSize.Height;

                StreamGeometry geom = new StreamGeometry();
                using (StreamGeometryContext ctx = geom.Open())
                        new Point(0.0, 0.0),
                        new Point(maxWidth, maxHeight),

                return geom;

We can use the shape in XAML as follows:

        <loc:MyShape Stroke="Black" StrokeThickness="1"
                     Height="50" Width="50"

Now when we change the size of the underlying control, the geometry adjusts as well.




About Sean
Software developer in the Twin Cities area, passionate about software development and sailing.

2 Responses to #1,145 – Using RenderSize in Custom Shape

  1. Pingback: Dew Drop – August 27, 2014 (#1843) | Morning Dew

  2. Pingback: Custom shapes in WPF | Around computing

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: