下面的滚动查看器不起作用。我尝试了在这个网站上可以找到的所有内容:将滚动查看器嵌入网格中,将ScrollViewer的子项嵌入到网格中,将Scrollviewer嵌入到具有固定高度的StackPanel中,设置/绑定滚动查看器的高度,一切都无济于事......谁向我展示了恢复理智的方式?
请注意,下面的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位置
答案 0 :(得分:4)
试试这个
<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>
修改强>
您的新代码正常工作,因为现在滚动查看器大小已修复(它填满了屏幕的免费部分),并且当内容不断增长时,它不会在窗口外增长...
答案 1 :(得分:1)
尝试在scrollviewer中为列表框或stackpanel指定高度,当你的内容大于它的大小时,当你向listbox添加项目时,scrollviewer会滚动,listbox的高度不会增长而listbox正在滚动