Question

Im new in WPF & Xaml I dont know how to anchor how to dock... On this screen gray is statusBar is docked but grid and menu is not all components are in canvas. This is XAML

<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Class="WpfApplication6.MainWindow"
x:Name="Window"
Title="MainWindow"
Width="640" Height="480">

<Grid x:Name="LayoutRoot">
    <Canvas>
        <StackPanel Height="40" Width="624" VerticalAlignment="Top" HorizontalAlignment="Center">
            <Menu Height="39" Margin="1,0,0,0">
                <Menu.Background>
                    <LinearGradientBrush EndPoint="0,1" StartPoint="0,0">
                        <GradientStop Color="#FFF6F6F6" Offset="0.25"/>
                        <GradientStop Color="#FFEAE8E8" Offset="0.25"/>
                        <GradientStop Color="#FFDCD9D9" Offset="0.8"/>
                        <GradientStop Color="White" Offset="1"/>
                    </LinearGradientBrush>
                </Menu.Background>
            </Menu>
        </StackPanel>
        <StackPanel Height="356" Canvas.Top="44" Width="161" HorizontalAlignment="Left">
            <Expander Header="Expander" Height="107">
                <Grid Background="#FFE5E5E5"/>
            </Expander>
        </StackPanel>
        <StackPanel Height="360" Canvas.Left="161" Canvas.Top="40" Width="463">
            <DataGrid Height="361"/>
        </StackPanel>
    </Canvas>
    <StackPanel Height="40" Margin="-1,0,0,0" VerticalAlignment="Bottom">
        <StatusBar Height="40" Background="#FF897676"/>
    </StackPanel>
</Grid>

alt text

Was it helpful?

Solution

You don't want these things to go in a Canvas. I'm not sure I've ever used a Canvas in WPF.

DockPanel is your friend, and this is how it works:

  • Each control in the DockPanel gets a DockPanel.Dock attached property : Left, Top, Right, or Bottom
  • What, no Fill? This is important: You can have one control fill up available space, and that will be the last control declared in the DockPanel. So, even if you want the "fill" control to be at the very top, you make it the last item in the DockPanel and set DockPanel.Dock="Top".
  • Other than the last, "fill" item, others which have the same dock setting will be docked in the order in which they were declared.

At its most basic, DockPanel can be used just like StackPanel except it will fill available space.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top