forum

Home / DeveloperSection / Forums / How to properly center a WPF canvas?

How to properly center a WPF canvas?

Anonymous User 2484 24-Sep-2013

I have a full screen dialog and want to center a canvas. It seems to work well with a grid with Vertical/Horizontalalignment for a Label for example, but when I try this the top left corner gets centered instead of the middle of the canvas:

<Window
    Title=""
    Topmost="True" WindowStyle="None" WindowState="Maximized"
    >
<Grid>
    <Grid VerticalAlignment="Center" HorizontalAlignment="Center">
        <Canvas>
            <Border Margin="20"
                Background="White"
                BorderBrush="Black"
                BorderThickness="2"
                Padding="20" >
                <DockPanel Margin="10">
                    <StackPanel DockPanel.Dock="Top" Margin="0 0 0 50"
                                Orientation="Vertical">
                       <Label FontSize="32" Content="Hello"></Label>
                    </StackPanel>
                    <StackPanel HorizontalAlignment="Right"
                                DockPanel.Dock="Bottom"
                                Orientation="Horizontal">
                            <Label FontSize="32" Content="Hello"></Label>
                    </StackPanel>
                </DockPanel>
            </Border>
        </Canvas>
    </Grid>

c# wpf 
Updated on 24-Sep-2013
I am a content writter !

Can you answer this question?


Answer

1 Answers

Liked By