在WPF中停靠顶部/底部/左/右/填充

Tho*_*mas 12 wpf

我是胜利形式开发者.每当我想在其容器中的顶部/底部/左/右位置设置任何控件时,我们只需在winform中播放控件dock属性.所以只需指导我如何将控件放在其容器顶部/底部/左/右位置,以便在包含尺寸更改时控制位置在wpf中不会改变.

搜索谷歌后,我开始知道如何填充Dock属性,它就像

<Window ...Other window props... >
    <Canvas HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
        <!-- Canvas items here... -->
    </Canvas>
</Window>
Run Code Online (Sandbox Code Playgroud)

因此,请指导我如何使用代码段在其容器中的顶部/底部/左侧/右侧位置设置任何控件.

UPDATE

我刚认识码头面板可以像我这样使用我的要求

<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>
Run Code Online (Sandbox Code Playgroud)

我可以在不使用DockPanel的情况下实现此目的.谢谢

AlS*_*Ski 15

你可以使用网格,(注意星号大小)

<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>
Run Code Online (Sandbox Code Playgroud)

您可以使用边距和对齐(边距是近似值)

<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>
Run Code Online (Sandbox Code Playgroud)

您可以使用StackPanels(这需要更多的工作来填充空间)

<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>
Run Code Online (Sandbox Code Playgroud)