0

I have a ScrollViewer, which contains a DockPanel, which contains a ListBox as its filled element. The problem I'm facing is that, when the ListBox contains many items, and the height of the window is reduced to the point where a scrollbar is necessary, the ScrollViewer's scrollbar appears, pushing my controls with DockPanel.Dock="Bottom" off the screen. The ListBox's scrollbar never appears. Instead, when the window height is reduced, I would like the ListBox's scrollbar to appear first. Then, after the ListBox shrinks to some minimum height that I specify, the ScrollViewer's scrollbar should appear to take care of the rest.

How do?

Paul Accisano
  • 1,416
  • 1
  • 14
  • 25
  • I don't have the time to check this but I think you should set a minimun height for the ListBox, and then bind the actual height of the ListBox to the actual height of the ScrollViewer with a converter to make the ListBox actual height shrink before the ScrollViewer's ScrollBar apear. Then only when the ListBox shrink to its minimal height the ScrollViewer's ScrollBar should apear. Hope it works. – Seffix Jan 09 '12 at 05:19

1 Answers1

1

You Should set the ListView's MinHeight property to the specific height which you want the ScrollViewer's ScrollBar to apear or being enabled. The ListView's Height Property should be bind to the ScrollViewer's Height Property. Then when the Window's Height is reduced to an Height which is hide some of the list in the ListView, the ListView's ScrollBar apears. when the ListView's Height reaches its MinHeight the ScrollViewer's ScrollBar apears.

This is The Xaml code:

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <ScrollViewer VerticalScrollBarVisibility="Auto">
            <DockPanel>
                <ListView x:Name="listView1" DockPanel.Dock="Bottom" MinHeight="100" Height="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=ScrollViewer, AncestorLevel=1}, Path=ActualHeight}"/>
            </DockPanel>
        </ScrollViewer>
    </Grid>
</Window>
Seffix
  • 1,049
  • 1
  • 12
  • 16
  • Unfortunately, I can't do this. The reason is that the DockPanel and ListView are part of a UserControl, so they cannot reference the ScrollViewer (which contains the UserControl). However, even if I could, I don't think this would work. It looks like it would cause the listview to be the same height as the scrollviewer, which would knock my other controls off the screen. – Paul Accisano Jan 09 '12 at 14:35
  • Second, You can use a converter to adjust the Height to what you want, so you can calculate the exact height which leave enough room for your other controls. – Seffix Jan 09 '12 at 15:57
  • Third, you can do so with binding the DockPanel itself the same way, to mediate between the scrollview and all of the content of the dockpanel. Then you can bind your whole control's Height to its container which, in your case, is the ScrollViewer. – Seffix Jan 09 '12 at 15:57
  • And last thing, I don't know exactly what your requirements are, but I gave you the Idea of the solution. – Seffix Jan 09 '12 at 15:58
  • Sorry, but this just isn't the solution I was looking for. It would require me to have a different converter each time I wanted to do this, and it breaks encapsulation by forcing me to make my UserControl assume it's in a ScrollViewer, even when it might not be. I'll give you an up-vote since your solution might help someone else, but I can't mark it as the answer. – Paul Accisano Jan 09 '12 at 19:23