ScrollViewer 在 WPF 中不滚动
我在堆栈面板周围使用滚动查看器控件,其中包含 ItemsControl。 当 ItemsControl 中有很多项目时,它应该滚动,但由于某种原因它只是剪切项目。 这是代码:
<StackPanel>
<ScrollViewer CanContentScroll="True" VerticalScrollBarVisibility="Visible">
<ItemsControl Name="icEvents" Width="Auto" Height="100" Background="AliceBlue"
ItemsSource="{Binding Path=EventSources}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<StackPanel>
<TextBlock Text="Source:"/>
<TextBlock Text="{Binding Path=Source}" />
<TextBlock Text="Original Source:"/>
<TextBlock Text="{Binding Path=OriginalSource}" />
</StackPanel>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</ScrollViewer>
</StackPanel>
I am using a scrollviewer control around my stack panel which contains an ItemsControl. When there are many items in the ItemsControl it is suppose to scroll but for some reason it just cuts of the items. Here is the code:
<StackPanel>
<ScrollViewer CanContentScroll="True" VerticalScrollBarVisibility="Visible">
<ItemsControl Name="icEvents" Width="Auto" Height="100" Background="AliceBlue"
ItemsSource="{Binding Path=EventSources}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<StackPanel>
<TextBlock Text="Source:"/>
<TextBlock Text="{Binding Path=Source}" />
<TextBlock Text="Original Source:"/>
<TextBlock Text="{Binding Path=OriginalSource}" />
</StackPanel>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</ScrollViewer>
</StackPanel>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
尝试在
ScrollViwer
周围使用网格,而不是StackPanel
。 我认为 StackPanel 将提供内部内容所需的高度,因此这里 Scrollviwer 无法正常工作,因为它的高度不受其父控件的限制。你可以通过下面的例子来理解这个问题。
上面的代码与您的类似,但它没有给您滚动条。 但请参阅下面的代码,其中我仅将
StackPanel
更改为Grid
(任何根据面板大小尊重其子级大小的面板,但 stackpanel 不会)< strong>更新:但是如果您确实需要使用
StackPanel
,那么您可能需要设置ScrollViwer
的大小才能获得内容滚动Try a grid around your
ScrollViwer
instead of theStackPanel
. I thinkStackPanel
will provide as much height as the internal content wants, so here theScrollviwer
doesn't work properly since its height is not get restricted by its parent control.You can understand the issue from the example below.
Above code is similar to yours and it doesn't give you scrollbars. But see the below code in which I changed only the
StackPanel
to aGrid
(Any panel which respects the size of its children based on panels size but stackpanel doesn't)UPDATE : But if you really need to use
StackPanel
then you might need to set the size for yourScrollViwer
to get the Content scroll您必须固定 Scrollviewer 的高度,但可以轻松绑定到 StackPanel ActualHeight:
(经过测试的代码)
或者更好的是,如果您无法更改 StackPanel 的名称:
这是一个“您优先”的问题,StackPanel 向 ScrollViewer 询问高度,ScrollViewer 向 StackPanel 询问它可以达到的最大高度。
You must fix the Height of the Scrollviewer, but can easily bind to the StackPanel ActualHeight:
(tested code)
Or better yet, if you can't change the name of the StackPanel:
It is a "You first" problem, the StackPanel ask the ScrollViewer for the Height and the ScrollViewer ask the StackPanel for the max Height it can be.
ItemsControl
应包含ScrollViewer
,而不是相反。ScrollViewer
所知道的只是您案例中的ItemsControl
本身 - 它不知道内部项目。尝试这样的事情:
The
ItemsControl
should contain theScrollViewer
, not the other way around. All that theScrollViewer
knows about is theItemsControl
itself in your case - it doesn't know about the inner items.Try something like this:
Scroll 需要是调整大小的子级的父级,因此,将调整大小控件(在本例中为堆栈面板)放入其中。
The Scroll needs to be the parent of the resizing child so, put the resizing control(in this instance the stack panel) inside it.
我遇到了一个问题,我在网格中显示了
ItemsControl
,为了看到滚动条,我必须给这个网格单元格 * 大小的高度。但是,这意味着即使只有几个值,
ItemsControl
也始终会填充单元格。我希望
ItemsControl
足够大以显示它所拥有的内容,但如果它有太多项目,那么我希望它滚动。 换句话说,我希望它只适用于任何尺寸的显示器。Loaded 事件中的这段代码对我有用:
I had a problem whereby I showed an
ItemsControl
in a Grid and in order to see the scrollbars, I had to give this Grid cell *-sized height.However, this meant that the
ItemsControl
always filled the cell even when there were only a few values.I wanted the
ItemsControl
to be only big enough to show what it had but if it had too many items then I wanted it to scroll. In order words I wanted this to work only any size monitor.This code in the Loaded event worked for me: