WPF中的停靠:顶部/底部/左侧/右侧/填充

15

我是一个WinForm开发者。每当我想把控件放在容器的顶部/底部/左侧/右侧时,我们只需在WinForm中玩弄控件的Dock属性。那么请指导我如何将一个控件放置在其容器的顶部/底部/左侧/右侧位置,以便当容器大小改变时,控件位置不会改变在WPF中。

在搜索谷歌后,我知道了如何使用Dock属性进行填充,就像这样:

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

请指导我如何使用代码片段将任何控件设置在其容器的顶部/底部/左侧/右侧位置。

更新

我刚刚知道可以使用停靠面板(Dock Panel)来满足我的要求,如下所示:

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

除了使用 DockPanel,还有其他方法可以实现这个吗?谢谢。


1
你可以使用 DockPanel 控件。 - UIlrvnd
2
为什么你不想使用DockPanel呢?这就是它存在的意义 :P... - UIlrvnd
DockPanel确实是正确的组件。您也可以使用简单的Grid,但如果您有一个定义其他元素位置的中心元素,则使用DockPanel更为适合。 - Arhiman
2个回答

17

你可以使用 Grid(注意星号大小)

<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>
你可以使用边距和对齐方式(这里的边距是大致的)。
<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>

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

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

谢谢您的回答。在WinForm中,我们使用锚定属性...在WPF中是否有类似的属性? - Thomas
听起来很像HorizontalAlignment和VerticalAlignment,可以参考这个链接的解释http://msdn.microsoft.com/en-us/library/ms751709.aspx里面也有HA和VA的链接。 - AlSki
我想问你一个问题,因为我觉得你在WPF方面很有经验。是否有任何网站专门提供大量的WPF示例代码,以开发光滑、吸引人的用户界面?我在谷歌上搜索了一下,但没有找到。如果你知道,请给我这些网址。谢谢。 - Thomas

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接