当滚动条在wpf中不可见时如何展开窗口内容?

发布于 2024-08-05 21:20:32 字数 1048 浏览 4 评论 0原文

我在 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 技术交流群。

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

发布评论

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

评论(3

铁憨憨 2024-08-12 21:20:32

当滚动条的可见性更改时,不会引发任何事件,但您可能可以挂钩 VerticalScrollBarVisibility 和/或 Horizo​​ntalScrollBarVisibility 依赖项属性的更改通知。也许你可以使用这个小片段。

DependencyPropertyDescriptor scrollbarDesc =
    DependencyPropertyDescriptor.FromProperty
    (ScrollViewer.VerticalScrollBarVisibilityProperty, typeof(Visibility));

if (scrollbarDesc != null)
{
    scrollbarDesc.AddValueChanged(scrollViewer1, delegate
    {
        // Add your propery changed logic here...
    });
}

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.

DependencyPropertyDescriptor scrollbarDesc =
    DependencyPropertyDescriptor.FromProperty
    (ScrollViewer.VerticalScrollBarVisibilityProperty, typeof(Visibility));

if (scrollbarDesc != null)
{
    scrollbarDesc.AddValueChanged(scrollViewer1, delegate
    {
        // Add your propery changed logic here...
    });
}
遮云壑 2024-08-12 21:20:32

Trainee4Life 有这个想法,但您只需要连接到另一个属性 [ScrollViewer.CompulatedVerticalScrollBarVisibilityProperty] 并将 typeof(Visisbility) 更改为 typeof(ScrollViewer) 即可。

DependencyPropertyDescriptor scrollbarDesc =
    DependencyPropertyDescriptor.FromProperty
    (ScrollViewer.ComputedVerticalScrollBarVisibilityProperty, typeof(ScrollViewer));

if (scrollbarDesc != null)
{
    scrollbarDesc.AddValueChanged(scrollViewer1, delegate
    {
        // Add your propery changed logic here...
    });
}

Trainee4Life has the idea, but you just need to hook into a different property [ScrollViewer.ComputedVerticalScrollBarVisibilityProperty] and change the typeof(Visisbility) to typeof(ScrollViewer).

DependencyPropertyDescriptor scrollbarDesc =
    DependencyPropertyDescriptor.FromProperty
    (ScrollViewer.ComputedVerticalScrollBarVisibilityProperty, typeof(ScrollViewer));

if (scrollbarDesc != null)
{
    scrollbarDesc.AddValueChanged(scrollViewer1, delegate
    {
        // Add your propery changed logic here...
    });
}
输什么也不输骨气 2024-08-12 21:20:32

你实际上已经找到了问题的答案。

您需要将滚动条的状态设置为 Collapsed 而不是 Invisible

scrollbar.Visibility = Collapsed;

这会删除元素,而不只是隐藏它并在 UI 中为其保留空间。

You've actually hit on the answer in your question.

You need to set the state of the scrollbar to Collapsed rather than Invisible:

scrollbar.Visibility = Collapsed;

This removes the element rather than just hiding it and reserving the space for it in the UI.

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