views:

68

answers:

2

Hello,

Can anyone tell me how to center a polygon object within a given row/column of a Grid control?

The example that I have tried is taken from msdn.

<Grid x:Name="LayoutRoot" >
  <Polygon Points="300,200 400,125 400,275 300,200" 
           Stroke="Purple" 
           StrokeThickness="2"
           HorizontalAlignment="Center"
           VerticalAlignment="Center" >
    <Polygon.Fill>
        <SolidColorBrush Color="Blue" Opacity="0.4" />
    </Polygon.Fill>
 </Polygon>

Cheers,

Xam

+1  A: 

Add the attributes :-

 HorizontalAlignment="Center" VerticalAlignment="Center"

to the Polygon.

AnthonyWJones
A: 

Although a height and width are implied by the bounds of the polygon, it defaults to the size of the container.

If you just set

HorizontalAlignment="Center" VerticalAlignment="Center"

it will position the polygon's top-left in the centre.

You also have to explicitly set the height and width of the polygon to centre it and retain its bounds

Sample polygon Xaml with dimensions added:

<Grid x:Name="LayoutRoot">
   <Path Data="M0.5,41.5 L201,0.5 L302,115 L157.25,157 z" Fill="#FFF4F4F5" Stroke="Black" UseLayoutRounding="False" HorizontalAlignment="Center" VerticalAlignment="Center" Width="302.5" Height="157.5"/>
</Grid>
Enough already
@HiTechMagic: How are you defining your container and polygon? I don't see it stretching with a simple grid and 4-point polygon in a quick example I threw together after seeing your post.
Wonko the Sane
@HiTechMagic: I have the same issue as Wonko. Despite following your direction, I can't get a simple polygon (in my case a triangle) to center within a Grid control.
Xam
Enough already
@HiTechMagic: Thanks again. Can you please provide a xaml snippet which demonstrates how you achieved this result?
Xam
@Xam: Xaml Added above. It would be more useful to see and correct your Xaml. Mine was a simple poly authored with the pen tool in Blend.
Enough already
@HiTechMagic: Thanks. I have added the sample above.
Xam