views:

371

answers:

3

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>
A: 

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.

ChrisF
what handler can I hook into to set this? Currently WPF handles it for me since I set VerticalScrollBarVisibility="Auto"... Is there an OnScrollBarVisibilityChanged event or similar?
bluebit
Not sure - it's a while since I looked at WPF.
ChrisF
+1  A: 

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...
    });
}
Trainee4Life
Thanks man! That definitely seems promising. I get an exception when I run the thing on the first line you pasted:ArgumentException: 'Visibility' type does not have a matching DependencyObjectType.
bluebit
I tried changing Visibility to ScrollBarVisibility, which it actually is, but still get the same error....
bluebit
A: 

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...
    });
}
spens007