WPF 对齐拉伸
有人可以帮我理解 WPF 拉伸对齐吗?我经常遇到这样的问题:我想要一个控件来填充一个区域,但我永远不明白应该将 VerticalAlignment="Stretch" 放在什么级别。特别是当涉及用户控件时。
我总是通过尝试不同的事情或在各个层面上进行扩展来解决问题,但我想了解如何正确地做到这一点。
让我们以我现在得到的例子为例:
- 我有一个带有固定大小单元格的网格(可以使用 GridSplitter 调整大小)
- 在这个单元格中,我有一个包含带有 TabControl 的 StackPanel 的 UserControl
- 在 TabControl 中,我有包含另一个带有 TabControl 的 UserControl 的 TabItems ListView
或者某种伪 XAML
<ns:MyUserControl1 Grid.Row="0" Grid.Column="0">
<!-- this is in MyUserControl1 -->
<StackPanel>
<TabControl>
<TabItem>
<ns:MyUserControl2>
<!-- This is in MyUserControl2 -->
<ListView/>
</ns:MyUserControl2>
</TabItem>
</TabControl>
</StackPanel>
</ns:MyUserControl>
现在我希望 ListView 填充整个网格单元(当然不包括 TabControl 和边距)。
Can someone help me understand the WPF stretch alignment. I often run into issues where I want a control to fill an area but I never understand on what level I should put the VerticalAlignment="Stretch". Specially when UserControls are involved.
I always solve the problem by trying different things or putting stretch on all levels but I would like to understand how to do it properly.
Lets take the example I got now:
- I have a grid with a fixed size cell (which can be resized with a GridSplitter)
- In this cell I have a UserControl containing a StackPanel with a TabControl
- In the TabControl I have TabItems containing another UserControl with a ListView
Or in some kind of pseudo XAML
<ns:MyUserControl1 Grid.Row="0" Grid.Column="0">
<!-- this is in MyUserControl1 -->
<StackPanel>
<TabControl>
<TabItem>
<ns:MyUserControl2>
<!-- This is in MyUserControl2 -->
<ListView/>
</ns:MyUserControl2>
</TabItem>
</TabControl>
</StackPanel>
</ns:MyUserControl>
Now I want the ListView to fill the entire grid cell (excluding the TabControl and margins of course).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的 ListView 将填充您的 MyUserControl2 - 如果您希望它适合整个选项卡,您需要确保删除 UserControl 的 xaml 文件中的高度和宽度约束(这些约束将默认为每个 300)
编辑:道歉 - I跳过了 xaml 的重要部分... MyUserControl1 容器是一个 StackPanel...这将堆叠子控件并调整大小以适合它们;如果您将其替换为 DockPanel(或 Grid),子控件将填充可用空间并为您提供所需的结果...
看看 这篇 msdn 文章...希望这会有所帮助:)
Your ListView will fill your MyUserControl2 - if you want this to fit within the whole Tab, you'll need to make sure you remove the Height and Width constraints within the UserControl's xaml file (these will default to 300 each)
EDIT: Apologies - I skipped the important part of your xaml... The MyUserControl1 container is a StackPanel... this will stack child controls and resize to fit them; If you replace it with a DockPanel (or a Grid), the child controls will fill the available space and give you the result you are after...
Have a look at this msdn article... Hope this helps :)