在 WPF 中停靠上/下/左/右/填充

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18229697/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 23:53:56  来源:igfitidea点击:

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

wpf

提问by Thomas

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.

我是赢形式开发人员。每当我想在其容器的顶部/底部/左侧/右侧位置设置任何控件时,我们只需在 winform 中播放控件停靠属性。所以请指导我如何将控件放置在其容器的顶部/底部/左侧/右侧位置,以便在包含大小更改时控制位置不会在 wpf 中发生变化。

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

在谷歌搜索后,我开始知道填充是如何与 Dock 属性一起工作的,就像

<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

我可以通过任何其他方式在不使用 DockPanel 的情况下实现这一点。谢谢

回答by AlSki

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)

你可以使用 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>

回答by KaluSinghRao

 <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>