简体   繁体   中英

ScrollViewer not scrolling

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???

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...

尝试在scrollviewer中为列表框或堆栈面板赋予高度,当内容大于其大小时scrollviewer滚动(当您向列表框添加项目时),列表框的高度没有增长并且列表框正在滚动

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM