我是一个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,还有其他方法可以实现这个吗?谢谢。