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>
4

3 回答 3

1

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

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

if (scrollbarDesc != null)
{
    scrollbarDesc.AddValueChanged(scrollViewer1, delegate
    {
        // Add your propery changed logic here...
    });
}
于 2009-09-17T18:39:54.483 回答
1

Trainee4Life 有这个想法,但您只需要挂钩到不同的属性 [ScrollViewer.ComputedVerticalScrollBarVisibilityProperty] 并将 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...
    });
}
于 2009-10-08T20:21:46.887 回答
0

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

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

scrollbar.Visibility = Collapsed;

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

于 2009-09-17T12:24:15.057 回答