Question

The scrollviewer below does not work. I tried everything I could find on this site and beyond: embedding the scrollviewer in an Grid, embedding the ScrollViewer's children in a grid, embedding the Scrollviewer in a StackPanel with fixed height, setting/binding the height of the scrollviewer, all to no avail... Who shows me the way back to sanity??

Mind, the XAML below is just to show how the window is structured. I removed all the data.

<Window>
    <Window.Resources>
        <DataTemplate x:Key="ColoringLabels">
        </DataTemplate>
    </Window.Resources>
    <DockPanel>
        <StatusBar DockPanel.Dock="Top">
            <StatusBarItem>
            </StatusBarItem>
        </StatusBar>
        <StackPanel Orientation="Vertical">
            <TextBox/>
            <Button>Hello World!</Button>
            <ScrollViewer>
                <StackPanel Orientation="Vertical">
                    <Label>Hola Mundo!</Label>
                    <ListBox ItemsSource="{Binding}">
                        <ListBox.ItemTemplate>
                            <DataTemplate>
                                <ListBox ItemsSource="{StaticResource ColoringLabels}"/>
                            </DataTemplate>
                        </ListBox.ItemTemplate>
                    </ListBox>
                    <ListBox Source="{Binding}"ItemTemplate="{StaticResource ColoringLabels}"/>
                </StackPanel>
            </ScrollViewer>
            <TextBlock/>
        </StackPanel>
    </DockPanel>
</Window>

EDIT:

I solved it by changing the XAML to:

<Window>
   <Window.Resources>
       <DataTemplate x:Key="ColoringLabels">
       </DataTemplate>
   </Window.Resources>
   <DockPanel>
       <StatusBar DockPanel.Dock="Top">
           <StatusBarItem>
           </StatusBarItem>
       </StatusBar>
       <ScrollViewer>
            <StackPanel Orientation="Vertical">
                <TextBox />
                <Button>Hello World!</Button>
                    <StackPanel Orientation="Vertical">
                        <Label>Hola Mundo!</Label>
                        <ListBox ItemsSource="{Binding}">
                            <ListBox.ItemTemplate>
                                <DataTemplate>
                                    <ListBox ItemsSource="{StaticResource ColoringLabels}"/>
                                </DataTemplate>
                            </ListBox.ItemTemplate>
                        </ListBox>
                        <ListBox Source="{Binding}"ItemTemplate="{StaticResource ColoringLabels}"/>
                    </StackPanel>
                <TextBlock/>
            </StackPanel>
        </ScrollViewer>
    </DockPanel>
</Window>

Why it is working now??? Perhaps because the ScrollViewer now gets to fill the LastChild position of the DockPanel???

Was it helpful?

Solution

try this

<Window x:Class="WpfApplication7.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1" Height="308" Width="527">
    <Window.Resources>
        <DataTemplate x:Key="ColoringLabels">
        </DataTemplate>
    </Window.Resources>
    <DockPanel LastChildFill="True">
        <StackPanel DockPanel.Dock="Top" HorizontalAlignment="Stretch">
            <StatusBar>
                <StatusBarItem>
                </StatusBarItem>
            </StatusBar>
            <TextBox/>
            <Button>Hello World!</Button>
        </StackPanel>
        <ScrollViewer>
            <StackPanel Orientation="Vertical" >
                <Label>Hola Mundo!</Label>
                <ListBox ItemsSource="{Binding}">
                    <ListBox.ItemTemplate>
                        <DataTemplate>
                            <ListBox />
                        </DataTemplate>
                    </ListBox.ItemTemplate>
                </ListBox>
                <ListBox />
            </StackPanel>
        </ScrollViewer>
        <TextBlock/>
    </DockPanel>

</Window>

EDIT
Your new code is working just becouse scrollviewer size is fixed now(it's fill the free part of screen), and it is not growing outside the window when it's content is growing...

OTHER TIPS

Try to give height to your listbox or stackpanel in scrollviewer, scrollviewer scrolls when it's content is bigger than it's size in your case when you add items to listbox, listbox's height is not growing and listbox is scrolling

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top