Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Alternative to absolute positioning of shapes' points

Tags:

c#

wpf

I was wondering if there was a way in WPF to create a shape (ex.: polygon, line, ...) but instead of using absolute positioning for the points of the shape, we could use something like percentage.

For example, instead of having a line with absolute positioning like this: (X1=0,Y1=50,X2=100,Y2=50), we could have a line with percentage values (0 to 1) like this (X1=0,Y1=0.5, X2=1, Y2=0.5 where 1 is equivalent to the size of the parent). So no matter what is the size of the parent of the shape, the shape would always be proportional to its parent.

That could be done with dependency properties, but I would find it much cleaner if there was a way to do it with something like I described. I hope I didn't miss something really obvious that does exactly that...

Thanks!

like image 319
Absolom Avatar asked Oct 02 '11 04:10

Absolom


1 Answers

You could achieve a similar effect if you scale it by applying a scale transform on your geometry the size of the control.

<Path Width="100" Height="100" Stroke="Red">
    <Path.Data>
        <LineGeometry  StartPoint="0 0" EndPoint="1 1">
            <LineGeometry.Transform>
                <ScaleTransform ScaleX="{Binding Path=Width, RelativeSource={RelativeSource FindAncestor, AncestorType=Path}}"
                                ScaleY="{Binding Path=Height, RelativeSource={RelativeSource FindAncestor, AncestorType=Path}}" />
            </LineGeometry.Transform>
        </LineGeometry>
    </Path.Data>
</Path>

This should draw a red line with absolute points (0, 0) to (100, 100).

like image 71
Jeff Mercado Avatar answered Oct 16 '22 05:10

Jeff Mercado