3

I have a multiple treeviews inside expanders, which can grow in height along with the content. But when the height gets larger than the window's size, it goes outside the window.

The obvious solution would be to set the MaxHeight of the Treeview, but I can't determine it easily, because the available height depends on

  • Window height
  • The other expanders (open/closed)

What do I need to change to make the treeview's height still grow automaticly, but never larger than the window's height?

<StackPanel CanVerticallyScroll="True" ClipToBounds="False" Height="Auto" Name="StackPanel2" Width="250" DockPanel.Dock="Left" Orientation="Vertical" VerticalAlignment="Top" CanHorizontallyScroll="False" Margin="5">
    <Border BorderThickness="0" CornerRadius="5" Padding="1" BorderBrush="Red" Margin="0,5" >
        <Expander Header="Expander3" Height="Auto" IsExpanded="False" Width="Auto" Margin="2" BorderThickness="1">
           <Grid  Height="Auto" Width="Auto" VerticalAlignment="Stretch" HorizontalAlignment="Stretch" >

            <TreeView Height="Auto" BorderThickness="1" Margin="0,0,0,0" Padding="7" VerticalAlignment="Stretch" HorizontalAlignment="Stretch" >
           </TreeView>

        </Grid>
    </Expander>
    </Border>
</StackPanel>
Maestro
  • 9,046
  • 15
  • 83
  • 116

1 Answers1

2

One way I can think of is to put the StackPanel in a ScrollViewer. This way make sure it never expands beyond the Height of the Window. You won't get a separate Scroll for each of your expanders (if they are in the same StackPanel?) but I'm not 100% sure what you're after here.

<ScrollViewer Name="stackPanelScrollViewer"
              Loaded="stackPanelScrollViewer_Loaded"
              VerticalScrollBarVisibility="Auto">
    <StackPanel CanVerticallyScroll="True" ClipToBounds="False" Height="Auto" Name="StackPanel2" Width="250" DockPanel.Dock="Left" Orientation="Vertical" VerticalAlignment="Top" CanHorizontallyScroll="False" Margin="5">
        <Border BorderThickness="0" CornerRadius="5" Padding="1" BorderBrush="Red" Margin="0,5" >
            <Expander Header="Expander3" Height="Auto" IsExpanded="False" Width="Auto" Margin="2" BorderThickness="1">
                <Grid  Height="Auto" Width="Auto" VerticalAlignment="Stretch" HorizontalAlignment="Stretch" >
                    <TreeView Name="treeView1" Height="Auto" BorderThickness="1" Margin="0,0,0,0" Padding="7" VerticalAlignment="Stretch" HorizontalAlignment="Stretch" >
                    </TreeView>
                </Grid>
            </Expander>
        </Border>
    </StackPanel>
</ScrollViewer>

A downside to this is that a TreeView has it's own ScrollViewer defined within its ControlTemplate so you won't be able to Scroll with the MouseWheel if the mouse is positioned over a TreeView. A workaround to this is to attach a MouseWheel event handler for each TreeView and make the Scroll from there

private void stackPanelScrollViewer_Loaded(object sender, RoutedEventArgs e)
{
    treeView1.AddHandler(MouseWheelEvent, new RoutedEventHandler(StackPanelMouseWheel), true);
    //treeView2.AddHandler(MouseWheelEvent, new RoutedEventHandler(StackPanelMouseWheel), true);
}
private void StackPanelMouseWheel(object sender, RoutedEventArgs e)
{
    MouseWheelEventArgs eargs = (MouseWheelEventArgs)e;
    double x = (double)eargs.Delta;
    double y = stackPanelScrollViewer.VerticalOffset;
    stackPanelScrollViewer.ScrollToVerticalOffset(y - x);
}
Fredrik Hedblad
  • 83,499
  • 23
  • 264
  • 266
  • Thanks for your answer! I also thought about this method, but I really need the scrollbar inside the treeview, not in the stackpanel or its expanders. So that if I have 3 expanders, only the last one will show the scrollbars in the treeview. But if it turns out that is impossible, i will mark your solution as the answer. – Maestro Dec 05 '10 at 23:48
  • @Joshua: See if I understand this correctly. Does this mean that the Height for the first and second treeview's will never expand beyond the Height for the Window? Otherwise, what should happend if the second TreeView expands so the third TreeView gets "pushed" outside of the Window? Are all the TreeView's in the same StackPanel? – Fredrik Hedblad Dec 06 '10 at 08:10
  • All the treeviews are in the same stackpanel. You are right that it might be possible that the second treeview also expands outside the window (making the third invisible), but in my app that's unlikely (but still possible). If the third treeview does an auto-size based on the remaining space, it's good enough for me ;) – Maestro Dec 06 '10 at 11:28