The perfect focus on WPF canvas

Since the canvas requires top / left for placement, if you want to center something, add a grid on the correct Canvas.Top with HorizontalAlignment = "Center", the best way to do this, or is there a better way

This snippet is a 150X300 canvas, with some content centered on the grid ....

<Canvas Width="150" Height="300">
    <Grid Canvas.Top="75" Width="106" HorizontalAlignment="Center">
        {whatever you want centered}
    </Grid>
</Canvas>
+3
source share
2 answers

Guy's solution works, but you may have to tweak the z-order and visibility if you are testing the hit hard.

( XAML) /, ( ) / . HorizontalAlignment/VerticalAlignment .

+7

, , , , :

<Grid>
    <Canvas Width="150" Height="300"/>
    <Button HorizontalAlignment="Center" VerticalAlignment="Center" Width="106" Content="Click"/>
</Grid>
+4

Source: https://habr.com/ru/post/1708759/


All Articles