当滚动条在wpf中不可见时如何展开窗口内容?
我在 wpf 中的 ItemsControl 周围有一个滚动条,仅当列表长于窗口大小时才可见。然而,当它被隐藏时,滚动条应该在的地方有一个空白区域。
如何删除这个空间并“折叠”滚动条?
<ScrollViewer VerticalScrollBarVisibility="Auto"
HorizontalAlignment="Stretch"
HorizontalContentAlignment="Left"
VerticalContentAlignment="Top"
HorizontalScrollBarVisibility="Disabled">
<ItemsControl ItemsSource="{Binding Path=ContactGroups}"
Width="Auto"
MinWidth="231"
MinHeight="342"
ScrollViewer.VerticalScrollBarVisibility="Disabled"
Height="Auto"
HorizontalContentAlignment="Left"
VerticalContentAlignment="Top"
HorizontalAlignment="Stretch">
<ItemsControl.ItemTemplate>
<DataTemplate>
<c:ContactGroupControl />
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</ScrollViewer>
I have a scrollbar in wpf around a ItemsControl, which is only visible when the list is longer than the window size. However, when it is hidden, there is a blank white space where the scrollbar should be.
How can I remove this space and "collapse" the scrollbar instead?
<ScrollViewer VerticalScrollBarVisibility="Auto"
HorizontalAlignment="Stretch"
HorizontalContentAlignment="Left"
VerticalContentAlignment="Top"
HorizontalScrollBarVisibility="Disabled">
<ItemsControl ItemsSource="{Binding Path=ContactGroups}"
Width="Auto"
MinWidth="231"
MinHeight="342"
ScrollViewer.VerticalScrollBarVisibility="Disabled"
Height="Auto"
HorizontalContentAlignment="Left"
VerticalContentAlignment="Top"
HorizontalAlignment="Stretch">
<ItemsControl.ItemTemplate>
<DataTemplate>
<c:ContactGroupControl />
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</ScrollViewer>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
当滚动条的可见性更改时,不会引发任何事件,但您可能可以挂钩 VerticalScrollBarVisibility 和/或 HorizontalScrollBarVisibility 依赖项属性的更改通知。也许你可以使用这个小片段。
There ain't any events which are raised when scrollbar's visibility is changed, but may be you could hook into change notifications of VerticalScrollBarVisibility and/or HorizontalScrollBarVisibility dependency properties. May be you could use this little snippet.
Trainee4Life 有这个想法,但您只需要连接到另一个属性 [ScrollViewer.CompulatedVerticalScrollBarVisibilityProperty] 并将 typeof(Visisbility) 更改为 typeof(ScrollViewer) 即可。
Trainee4Life has the idea, but you just need to hook into a different property [ScrollViewer.ComputedVerticalScrollBarVisibilityProperty] and change the typeof(Visisbility) to typeof(ScrollViewer).
你实际上已经找到了问题的答案。
您需要将滚动条的状态设置为
Collapsed
而不是Invisible
:这会删除元素,而不只是隐藏它并在 UI 中为其保留空间。
You've actually hit on the answer in your question.
You need to set the state of the scrollbar to
Collapsed
rather thanInvisible
:This removes the element rather than just hiding it and reserving the space for it in the UI.