下面的滚动查看器不起作用。我尝试了在这个网站和其他网站上能找到的所有东西:在网格中嵌入scrollviewer,在网格中嵌入scrollviewer的子项,在固定高度的StackPanel中嵌入Scrollviewer,设置/绑定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位置?
发布于 2009-08-16 16:41:28
尝尝这个
<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的大小现在是固定的(它填满了屏幕的空闲部分),当它的内容在增长时,它不会在窗口外增长……
发布于 2009-08-16 16:14:31
尝试在滚动查看器中为列表框或堆叠面板赋予高度,当内容大于您的案例中的大小时,滚动查看器会滚动。当您向列表框添加项目时,列表框的高度不会增长,而列表框正在滚动
https://stackoverflow.com/questions/1284600
复制相似问题