[英]ScrollViewer not scrolling
下面的滾動查看器不起作用。 我嘗試了在該站點上以及在此站點以外所有可以找到的一切:將ScrollViewer嵌入到Grid中,將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位置???
嘗試這個
<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的大小現在已固定(它已填滿屏幕的自由部分),並且當內容增長時它不會在窗口外增長...
嘗試在scrollviewer中為列表框或堆棧面板賦予高度,當內容大於其大小時scrollviewer滾動(當您向列表框添加項目時),列表框的高度沒有增長並且列表框正在滾動
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.