Silverlight:无法让 ScrollViewer 和 WrapPanel 一起工作

发布于 2024-07-20 21:42:26 字数 857 浏览 4 评论 0原文

我正在使用 WrapPanel 来格式化一些文本。 在运行时,我将 TextBlock 和 StackPanel 添加为子项。 显然我需要一个滚动条,具体取决于数据大小。 在网上搜索时,我发现了多个答案,它们都建议在 WrapPanel 周围放置一个 ScrollViewer。 这对我来说很有意义,但我无法让它发挥作用。 这是我的代码:

scrollView = new ScrollViewer();
scrollView.HorizontalScrollBarVisibility = ScrollBarVisibility.Disabled;
scrollView.VerticalScrollBarVisibility = ScrollBarVisibility.Auto;
scrollView.HorizontalAlignment = HorizontalAlignment.Stretch;
scrollView.VerticalAlignment = VerticalAlignment.Stretch;
scrollView.Margin = new Thickness(0);
scrollView.BorderThickness = new Thickness(0);  

textPanel = new WrapPanel();
textPanel.Width = Width;
scrollView.Content = textPanel;

那不起作用。 ScrollViewer 似乎随着包含的 WrapPanel 一起增长。 如果我将垂直滚动条设置为可见,我可以看到滚动条随着内容而增长。 但是 ScrollViewer 从包含的窗口中增长出来,因此永远不会显示滚动条。

有什么提示我可能做错了什么吗?

干杯, 阿希姆

I'm using a WrapPanel to format some text. During runtime I add TextBlocks and StackPanels as Children. Obviously I need a scrollbar depending on the data size. Searching the web I found multiple answers which all propose to put a ScrollViewer around the WrapPanel. That makes sense to me, but I cannot get it to work. Here's my code:

scrollView = new ScrollViewer();
scrollView.HorizontalScrollBarVisibility = ScrollBarVisibility.Disabled;
scrollView.VerticalScrollBarVisibility = ScrollBarVisibility.Auto;
scrollView.HorizontalAlignment = HorizontalAlignment.Stretch;
scrollView.VerticalAlignment = VerticalAlignment.Stretch;
scrollView.Margin = new Thickness(0);
scrollView.BorderThickness = new Thickness(0);  

textPanel = new WrapPanel();
textPanel.Width = Width;
scrollView.Content = textPanel;

That does not work. The ScrollViewer seems to grow with the contained WrapPanel. If I set the vertical scroll bar to visible, I can see that the scrollbar grows with the content. But the ScrollViewer grows out of the containing window and therefor no scrollbar is displayed ever.

Any hint what I might be doing wrong?

cheers,
Achim

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

慢慢从新开始 2024-07-27 21:42:26

您需要一些东西来限制 WrapPanel 和 ScrollViewer 的大小 - 您可以设置查看器的宽度和高度,然后在 WrapPanel 上使用元素绑定:

 <ScrollViewer x:Name="ScrollViewer1" 
Width="200" 
Height="200"  
ScrollViewer.VerticalScrollBarVisibility="Visible"
  ScrollViewer.HorizontalScrollBarVisibility="Visible" >
                <controls:WrapPanel 
                      Width="{Binding ElementName=ScrollViewer1, Path=Width}" 
                      Height="{Binding ElementName=ScrollViewer1, Path=Height}">

希望有帮助。

伊恩

You need something to contrain the size of the WrapPanel and the ScrollViewer - you could set the width and height of the viewer then use an element binding on the wrappanel:

 <ScrollViewer x:Name="ScrollViewer1" 
Width="200" 
Height="200"  
ScrollViewer.VerticalScrollBarVisibility="Visible"
  ScrollViewer.HorizontalScrollBarVisibility="Visible" >
                <controls:WrapPanel 
                      Width="{Binding ElementName=ScrollViewer1, Path=Width}" 
                      Height="{Binding ElementName=ScrollViewer1, Path=Height}">

Hope that helps.

Ian

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文