简体   繁体   English

ScrollViewer无法滚动

[英]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?? 我尝试了在该站点上以及在此站点以外所有可以找到的一切:将ScrollViewer嵌入到Grid中,将ScrollViewer的子级嵌入到网格中,将Scrollviewer嵌入具有固定高度的StackPanel中,设置/绑定scrollviewer的高度,但无济于事...谁向我展示了恢复理智的道路?

Mind, the XAML below is just to show how the window is structured. 请注意,下面的XAML只是为了展示窗口的结构。 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: 我通过将XAML更改为:

<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??? 也许是因为ScrollViewer现在可以填充DockPanel的LastChild位置???

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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