Search code examples
wpfxamlgeometrypathgeometry

Position a WPF Path so that its origin is located at the bottom, center of its container


I'm trying to center a Path so that its origin (0, 0) is located at the bottom, center of its container. Assume that the container is a Grid.


Example:

Tail of arrow is at the center-bottom of the box

Note: the tail of the arrow is at the origin (0, 0). The tail is centered horizontally but the overall arrow is skewed to the left. This is what I want to achieve regardless of which direction the arrow is pointing.


This needs to work for paths where the x and y coordinates are positive, negative, or a mixture of both.

How can this be done via XAML with the least markup?


Solution

  • Here's what I ended up going with. Seems to work, but if there's a cleaner way to do this, I'd love to hear it:

    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*" />
            <ColumnDefinition Width="*" />
        </Grid.ColumnDefinitions>
        <Path Grid.Column="1"
              HorizontalAlignment="Left"
              VerticalAlignment="Bottom"
              StrokeThickness="1"
              Data="{Binding PathGeometry}" />
    </Grid>