代码之家  ›  专栏  ›  技术社区  ›  Dabblernl

滚动查看器不滚动

  •  2
  • Dabblernl  · 技术社区  · 16 年前

    下面的ScrollViewer不工作。我尝试了所有我能在这个网站上找到的东西,包括:将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的最后一个子位置了????

    2 回复  |  直到 15 年前
        1
  •  4
  •   Arsen Mkrtchyan    16 年前

    试试这个

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

    编辑
    您的新代码正在工作,因为ScrollViewer的大小现在是固定的(它填充了屏幕的空闲部分),并且当其内容增长时,它不会在窗口外增长…

        2
  •  1
  •   Arsen Mkrtchyan    16 年前

    尝试在ScrollViewer中为ListBox或StackPanel赋予高度,当内容大于其大小时,ScrollViewer会滚动;在您的示例中,当您向ListBox添加项目时,ListBox的高度不会增长,并且ListBox正在滚动。