Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Docking Top/Bottom/Left/Right/Filling in WPF

Tags:

wpf

i am win form developer. whenever i want to set any control at top/Bottom/Left/Right position in its container then we just play the controls dock property in winform. so just guide me how can i place a control in its container top/Bottom/Left/Right position in such a way as a result when contain size change then control position will not change in wpf.

after searching google i came to know how filling works with Dock property and it is like

<Window ...Other window props... >
    <Canvas HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
        <!-- Canvas items here... -->
    </Canvas>
</Window>

So guide me how to set any control at top/Bottom/Left/Right position in its container with code snippet.

UPDATE

i just come to know dock panel can be use for my requirement like this way

<DockPanel LastChildFill="True">
    <Button Content="Dock=Top" DockPanel.Dock="Top"/>
    <Button Content="Dock=Bottom" DockPanel.Dock="Bottom"/>
    <Button Content="Dock=Left"/>
    <Button Content="Dock=Right" DockPanel.Dock="Right"/>
    <Button Content="LastChildFill=True"/>
</DockPanel>

any other way can i achieve this without using DockPanel. thanks

like image 899
Thomas Avatar asked Aug 14 '13 10:08

Thomas


2 Answers

You can use a Grid, (note the star sizing)

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto"/>
        <RowDefinition Height="*"/>
        <RowDefinition Height="Auto"/>
    </Grid.RowDefinitions>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="Auto"/>
        <ColumnDefinition Width="*"/>
        <ColumnDefinition Width="Auto"/>
    </Grid.ColumnDefinitions>
    <!-- If not specified, then Grid.Column="0" Grid.Row="0" are defaults-->
    <Button Content="Dock=Top" Grid.ColumnSpan="3"/>
    <Button Content="Dock=Bottom" Grid.Row="2" Grid.ColumnSpan="3"/>
    <Button Content="Dock=Left" Grid.Row="1"/>
    <Button Content="Dock=Right" Grid.Column="2" Grid.Row="1" />
    <Button Content="LastChildFill=True" Grid.Column="1" Grid.Row="1"/>
</Grid>

You can use margins and alignments (margins are approximate here)

<Grid>
    <Button Content="Dock=Top" VerticalAlignment="Top"/>
    <Button Content="Dock=Bottom" VerticalAlignment="Bottom"/>
    <Button Content="Dock=Left" HorizontalAlignment="Left" Margin="0,35"/>
    <Button Content="Dock=Right" HorizontalAlignment="Right" Margin="0,35" />
    <Button Content="LastChildFill=True" Margin="75,35"/>
</Grid>

You can use StackPanels (this one needs more work to make it fill the space)

<StackPanel Orientation="Vertical" VerticalAlignment="Center" HorizontalAlignment="Center">        
    <Button Content="Dock=Top" />
    <StackPanel Orientation="Horizontal" >
    <Button Content="Dock=Left" />
        <Button Content="LastChildFill=True" />
        <Button Content="Dock=Right" />
    </StackPanel>
    <Button Content="Dock=Bottom" />
</StackPanel>
like image 163
AlSki Avatar answered Nov 02 '22 10:11

AlSki


 <DockPanel>
    <Button DockPanel.Dock="Left" Content="Left"></Button>
    <Button DockPanel.Dock="Right" Content="Right"></Button>

    <Button DockPanel.Dock="Top" Content="Top"></Button>
    <Button DockPanel.Dock="Bottom" Content="Bottom"></Button>
    <Button DockPanel.Dock="Left" Content="Center" HorizontalAlignment="Center"></Button>

</DockPanel>
like image 43
KaluSinghRao Avatar answered Nov 02 '22 11:11

KaluSinghRao