0

I have a treeview with a scrollviewer. When the treeview becomes to full with treenodes i want the scrollbar to show up. But no matter how big the treeview gets it never shows up. The treeview grows outside of window without limiting itself to staying inside window.

The structure is currently: MainWindow contains a Frame that displays a page, the page contains a usercontrol, the usercontrol contains a treeview.

The usercontrol is set to "stretch", but instead of stretching to fit it's parent (limiting the space and thus making scrollbar appear), it stretches to fit all it's children (and thus stretching outside of screen).

How would i go on about making the "auto" sizing limiting itself to it's parent, instead of making it showing all children?

How i would like it to look/work like

How it actually looks/works like

Code, kinda irrelevant, i just wanna know how i should apply scrollview:

MainWindow:

        <Frame Source="View/Pages/StartPage.xaml" x:Name="MainFrame" NavigationUIVisibility="Hidden"/>
    </DockPanel>

MainPage:

<Grid>
<Frame Source="ParamFrameV.xaml" x:Name="pageFrame" HorizontalAlignment="Center" VerticalAlignment="Stretch"/>

ParamFrameV (the usercontrol):

    <Grid>
<ScrollViewer VerticalScrollBarVisibility="Visible" HorizontalScrollBarVisibility="Disabled" VerticalAlignment="Top" MaxHeight="I WANNA LIMIT HEIGHT SO THAT IT WONT GO OFF SCREEN" Width="283">
        <TreeView ItemsSource="{Binding Nodes}" ItemContainerStyle="{StaticResource TreeViewItemExpandedStyle}">
        //irrelevant code for this question
        </TreeView>
</ScrollViewer>
  • If you set or bind the `Frame.Height` then all should work. – Rekshino Dec 13 '21 at 11:01
  • what you are basically trying to do is hide the original scrollViewer of the tree view and add a seperate scrolviewer to replace the tree views one. Two ways to do this eigther in code because pure xaml doesnt support binding scrollindexes or similar https://stackoverflow.com/questions/15151974/synchronized-scrolling-of-two-scrollviewers-whenever-any-one-is-scrolled-in-wpf or give the treeview items a padding that creates the illusion of the scrolviewer moving all the way outside of the treeview – Denis Schaf Dec 13 '21 at 11:56
  • Rekshino) bind to what? im not sure what you mean. Denis) hmm, but i cant get the scrollviewer to work even on the treeview. even when doing: " " it doesnt work. The scrollbar shows up but is grayed out, no matter how many items in treeview, the scrollbar doesnt work. The treeview still grows outside of window, instead of limiting it's size to the parent container. Parent container has no bearing on the behavior of the treeview/scrollview – Kevin Uchiha Dec 13 '21 at 12:02
  • i put down an aswer that kinda loopks like what you are trying to do see if that helps. I simplified your code and made it a minimal reproducable example as i ofc dont have your custom controls and itemsoruces – Denis Schaf Dec 13 '21 at 12:08

1 Answers1

-1

that works just fine for me:

enter image description here

<Grid>
    <ScrollViewer Height="{Binding ElementName=ParentGridName, Path=ActualHeight"}" VerticalScrollBarVisibility="Visible" HorizontalScrollBarVisibility="Disabled" VerticalAlignment="Top" Width="283">
        <TreeView Width="50" ScrollViewer.VerticalScrollBarVisibility="Disabled">
            <TreeViewItem Header="hi"></TreeViewItem> 
            <TreeViewItem Header="hi2"></TreeViewItem> 
            <TreeViewItem Header="hi3"></TreeViewItem> 
            <TreeViewItem Header="hi4"></TreeViewItem> 
            <TreeViewItem Header="hi5"></TreeViewItem> 
            <TreeViewItem Header="hi6"></TreeViewItem> 
            <TreeViewItem Header="hi7"></TreeViewItem> 
            <TreeViewItem Header="hi8"></TreeViewItem> 
        </TreeView>
    </ScrollViewer>
</Grid>

Note: scroling on the treeview itself will not scroll as it will capture your scroll event but i bet there is a way around this

Denis Schaf
  • 2,478
  • 1
  • 8
  • 17
  • It works for me like how it works for you, but it's not what i want. This way the height limit is 50, and thus if the items inside is combined over 50 then scrollbar will show up. Problem is that i do not have fixed size. If i resize the window then i want to be able to see more of the treeview. The treeview need to LIMIT it's height to the size of it's parent control. Take a look at the "How it actually looks/works like", the treeview's height becomes bigger than the height of the usercontrol that contains teh treeview. I need to limit the height in order to have the effect i want. – Kevin Uchiha Dec 13 '21 at 12:15
  • i edited the answer you can consider binding the heigt to the parent container – Denis Schaf Dec 13 '21 at 13:11
  • `Binding ParentGridName, Path=ActualHeight` won't work. It is not even a valid binding expression. Use ElementName or a RelativeSourceBinding. – Clemens Dec 13 '21 at 13:33
  • @Clemens jup you're right, updated – Denis Schaf Dec 13 '21 at 14:33
  • Note however that such a Binding is almost never necessary and should not be recommended. Assuming the Grid parent of the ScrollViewer is resized correctly - by layout - it would also resize its child element. We do however know nothing about that Grid - e.g. whether it is the top-level element in the UserControl's XAML, or if there are perhaps other parent elements that somehow disturb the standard layout mechanisms. – Clemens Dec 13 '21 at 15:01
  • I agree most of the time when using names in XAML for size and postioning reasons there is a smarter way around the underlaying issue if you just let layout containers do their thing – Denis Schaf Dec 13 '21 at 15:32