views:

26

answers:

1

I have an ScrollViewer that I am trying to do data binding on the height of. The ScrollViewer holds a long ListBox

So here is my question. My ScrollViewer will bind to the height of my window (Name="MainForm") just fine. But then it is too long.

If I try to bind to a grid in the window (Name="MainGrid") then the ScrollViewer expands to the full length of the listbox (no scrolling).

Obviously I could hard code the height, but then it will not resize with the window. A feature that I do not want to loose.

Any Ideas?

This is the xaml with the binding set to MainGrid

<Window 
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="WI Assistant" Height="621.25" Width="943.75" Name="MainForm" FontSize="14">
<Grid Name="MainGrid">
    <DockPanel Margin="266.25,0,455,12" HorizontalAlignment="Left" Name="dockPanel1">
        <StackPanel>
           <ScrollViewer  Height="{Binding ElementName=MainGrid, Path=Height}">
     <ListBox Name="cboProjects"  FontSize="14" >
                   <ListBoxItem>Test1</ListBoxItem> <ListBoxItem>Test1</ListBoxItem> <ListBoxItem>Test1</ListBoxItem>
                   <ListBoxItem>Test1</ListBoxItem> <ListBoxItem>Test1</ListBoxItem> <ListBoxItem>Test1</ListBoxItem>
                   <ListBoxItem>Test1</ListBoxItem> <ListBoxItem>Test1</ListBoxItem> <ListBoxItem>Test1</ListBoxItem>
                   <ListBoxItem>Test1</ListBoxItem> <ListBoxItem>Test1</ListBoxItem> <ListBoxItem>Test1</ListBoxItem>
                   <ListBoxItem>Test1</ListBoxItem> <ListBoxItem>Test1</ListBoxItem> <ListBoxItem>Test1</ListBoxItem>
                   <ListBoxItem>Test1</ListBoxItem> <ListBoxItem>Test1</ListBoxItem> <ListBoxItem>Test1</ListBoxItem>
                   <ListBoxItem>Test1</ListBoxItem> <ListBoxItem>Test1</ListBoxItem> <ListBoxItem>Test1</ListBoxItem>
                   <ListBoxItem>Test1</ListBoxItem> <ListBoxItem>Test1</ListBoxItem> <ListBoxItem>Test1</ListBoxItem>
                   <ListBoxItem>Test1</ListBoxItem> <ListBoxItem>Test1</ListBoxItem> <ListBoxItem>Test1</ListBoxItem>
                   <ListBoxItem>Test1</ListBoxItem> <ListBoxItem>Test1</ListBoxItem> <ListBoxItem>Test1</ListBoxItem>
                   <ListBoxItem>Test1</ListBoxItem> <ListBoxItem>Test1</ListBoxItem> <ListBoxItem>Test1</ListBoxItem>
                   <ListBoxItem>Test1</ListBoxItem> <ListBoxItem>Test1</ListBoxItem> <ListBoxItem>Test1</ListBoxItem>
                   <ListBoxItem>Test1</ListBoxItem> <ListBoxItem>Test1</ListBoxItem> <ListBoxItem>Test1</ListBoxItem>
                   <ListBoxItem>Test1</ListBoxItem> <ListBoxItem>Test1</ListBoxItem> <ListBoxItem>Test1</ListBoxItem>
                   <ListBoxItem>Test1</ListBoxItem> <ListBoxItem>Test1</ListBoxItem> <ListBoxItem>Test1</ListBoxItem>
                   <ListBoxItem>Test1</ListBoxItem> <ListBoxItem>Test1</ListBoxItem> <ListBoxItem>Test1</ListBoxItem>
                   <ListBoxItem>Test1</ListBoxItem> <ListBoxItem>Test1</ListBoxItem> <ListBoxItem>Test1</ListBoxItem>
                   <ListBoxItem>Test1</ListBoxItem> <ListBoxItem>Test1</ListBoxItem> <ListBoxItem>Test1</ListBoxItem>
                   <ListBoxItem>Test1</ListBoxItem> <ListBoxItem>Test1</ListBoxItem> <ListBoxItem>Test1</ListBoxItem>
                   <ListBoxItem>Test1</ListBoxItem> <ListBoxItem>Test1</ListBoxItem> <ListBoxItem>Test1</ListBoxItem>
                   <ListBoxItem>Test1</ListBoxItem> <ListBoxItem>Test1</ListBoxItem> <ListBoxItem>Test1</ListBoxItem>
                   <ListBoxItem>Test1</ListBoxItem> <ListBoxItem>Test1</ListBoxItem> <ListBoxItem>Test1</ListBoxItem>
                   <ListBoxItem>Test1</ListBoxItem> <ListBoxItem>Test1</ListBoxItem> <ListBoxItem>Test1</ListBoxItem>
               </ListBox>
           </ScrollViewer>
        </StackPanel>
    </DockPanel>
</Grid>

(This will run (F5) in XamlPadx)

+3  A: 

Take it out of the StackPanel, because StackPanels grow unrestricted in the direction of their orientation (vertically, in this case). Start with the simplest possible solution:

<Window>
    <ScrollViewer>
        <ItemsControl>
            ...
        </ItemsControl>
    </ScrollViewer>
</Window>

Then add complexity as required.

HTH, Kent

Kent Boogaart
+1 Using `StackPanel` with single elements tend to cause these questions often.
Will Eddins
Bless you! I was about to pull my hair out on this one. By the way, I was using the stackpanel to stack Expanders (more than 1 control). I simplified my Xaml before posting. Now I know I cannot use a ListBox inside an expander inside a stack panel. Thanks!
Vaccano
Or really, a ListBox inside a StackPanel (Expanders aside)
Vaccano
+1 Scrollviewers in Stackpanels as a rule don't work, I tore may a hair out because of this too
Dabblernl
Whoops. I changed my answer slightly to have an `ItemsControl` in the `ScrollViewer` rather than `ListBox`. That's because the template for a `ListBox` already has a `ScrollViewer` in it, so the external `ScrollViewer` is useless.
Kent Boogaart