【问题标题】:ScrollViewer not scrollingScrollViewer 不滚动
【发布时间】:2010-11-20 01:17:48
【问题描述】:

下面的滚动查看器不起作用。我尝试了在本网站及其他网站上可以找到的所有内容:将 scrollviewer 嵌入到网格中,将 ScrollViewer 的子代嵌入到网格中,将 Scrollviewer 嵌入到具有固定高度的 StackPanel 中,设置/绑定 scrollviewer 的高度,一切都无济于事...谁告诉我恢复理智的路??

请注意,下面的 XAML 只是为了展示窗口的结构。我删除了所有数据。

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

编辑:

我通过将 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>

为什么它现在可以工作???也许是因为 ScrollViewer 现在可以填充 DockPanel 的 LastChild 位置???

【问题讨论】:

  • 我现在遇到了同样的问题,我尝试了您的解决方案。但是为什么我的不工作?一旦我将列表框与滚动查看器一起使用,它就会变得一团糟。

标签: .net wpf xaml scrollviewer


【解决方案1】:

试试这个

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

编辑
您的新代码可以正常工作,因为滚动查看器的大小现在是固定的(它填充了屏幕的空闲部分),并且当它的内容增长时它不会在窗口之外增长......

【讨论】:

  • 感谢您为此付出的所有努力!我遵循了您之前的建议,并在滚动查看器下方的第一个 stackpanle 中添加了高度(没有发生任何事情),并为列表框提供了高度。这有效,但没有产生预期的效果。我将 Scrollviewer 放在层次结构中的较高位置,这确实产生了我正在寻找的东西。
  • 你好,我编辑了帖子,找到你上一个问题的答案。
【解决方案2】:

尝试在滚动查看器中为您的列表框或堆栈面板指定高度,当您将项目添加到列表框时,滚动查看器的内容大于其大小时滚动,列表框的高度没有增长并且列表框正在滚动

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-04-20
    • 2017-01-02
    • 2010-11-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多