Search code examples
c#wpfxamlalignmentsize

horizontal and vertical Stretch in Canvas


I'm developing an app using WPF c#. I set Width=700 and Height=700 for a Window and my Window have a Grid, Border and Canvas which are nested. Horizonta and vertical Alignment for Border and Canvas are Stretch. It is expected that ActualWidth and ActualHeight of Canvas be 700 but they are 682 and 659. What is wrong? Am I missing setting up some properties?

I use this xaml code:

<Window x:Class="proj.Window1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Window1" Height="700" Width="700" WindowStartupLocation="CenterScreen" Loaded="Window_Loaded">
    <Grid>
        <Border BorderBrush="Black" BorderThickness="1" HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
            <Canvas x:Name="cnv" HorizontalAlignment="Stretch" VerticalAlignment="Stretch">

            </Canvas>
        </Border>
    </Grid>
</Window>

And here is "Window_Loaded" event code:

private void Window_Loaded(object sender, RoutedEventArgs e)
{
    MessageBox.Show(cnv.ActualWidth + "\n" + cnv.ActualHeight);
}

Solution

  • I suppose that the Height of the Window include the title bar on the top. So if you set 700, the real effective height of the Window is 682, because the title bar takes 18 pixels. Same behavior for the Width, because any Window have a border on the left and right.