WPF DockPanel 不尊重“填充”面板控件所需的大小
我有以下 Xaml(为简洁起见进行了简化,但会在 Xamlpad 或 Kaxaml 中重现问题):
<DockPanel Width="400">
<TextBlock DockPanel.Dock="Left" TextWrapping="Wrap">
Lorem ipsum dolor sit amet, consectetur adipiscing elit.
Vestibulum massa metus, ornare in fringilla nec, fringilla at orci.
Nunc pharetra enim sit amet sapien aliquet eu euismod turpis vehicula.
Aenean gravida tempus lectus ut ornare.
Nullam massa augue, suscipit vel consectetur fringilla, pretium vitae neque.
</TextBlock>
<Button MinWidth="100" VerticalAlignment="Center" HorizontalAlignment="Left">Hello world</Button>
</DockPanel>
我的问题是我希望按钮占用最小 100px 的空间,并让文本适当换行以留下该空间。然而,发生的情况是文本尽可能接近 400 像素换行,然后按钮被剪裁。
如果我监听输出,我可以看到按钮正在以所需的 100 像素渲染,但它被从 DockPanel 的一侧剪掉。
如果我反转 Dock(因此按钮停靠在“右侧”并且 TextBlock 填充),那么我会得到我想要的布局,但不幸的是,由于周围的布局,这不是一个选项。
我可以做些什么来使 DockPanel a) 不剪辑和 b) 布局以尊重 MinWidth 的方式吗?或者我是否一直在寻找替代布局机制?
提前致谢!
I have the following Xaml (simplified for brevity, but will repro the problem in Xamlpad or Kaxaml):
<DockPanel Width="400">
<TextBlock DockPanel.Dock="Left" TextWrapping="Wrap">
Lorem ipsum dolor sit amet, consectetur adipiscing elit.
Vestibulum massa metus, ornare in fringilla nec, fringilla at orci.
Nunc pharetra enim sit amet sapien aliquet eu euismod turpis vehicula.
Aenean gravida tempus lectus ut ornare.
Nullam massa augue, suscipit vel consectetur fringilla, pretium vitae neque.
</TextBlock>
<Button MinWidth="100" VerticalAlignment="Center" HorizontalAlignment="Left">Hello world</Button>
</DockPanel>
My problem is that I want the Button to take its minimum 100px of space, and for the text to wrap suitably to leave that space. However, what happens is that the text wraps as close to 400px as possible, and then the Button is clipped.
If I Snoop the output I can see that the button is rendering at the desired 100px, but it's being clipped off the side of the DockPanel.
If I reverse the Dock (so the button is docked "Right" and the TextBlock fills) then I get the layout I want, but unfortunately that's not an option due to the surrounding layout.
Is there something I can do that will make the DockPanel a) not clip and b) layout in a way that respects the MinWidth? Or am I stuck finding an alternative layout mechanism?
Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
啊,我傻了,盯着同一段代码太久了。我太简单的解决方案是使用网格:
为我自己的愚蠢问题道歉!
Ah, I am dumb, and had been staring at the same code for too long. My far-too-simple solution was to use a Grid:
Apologies for my own silly question!
您应该将按钮停靠在右侧,并让文本块填充其余部分。这是相应的 xaml:
最好的问候,
奥利弗·哈纳皮
You should dock the button to the right and let the textblock fill the rest. Here is the corresponding xaml:
Best Regards,
Oliver Hanappi