2011-07-14 83 views
3

我想在我的程序中使用一個簡單的ScrollViewer,但我遇到了問題。WPF ScrollViewer問題

如果我在一個ScrollViewer中附上我的程序中的一切,它工作正常:

<Window x:Class="WpfTest.MainWindow"   
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     Title="MainWindow" 
     Name="PrimaryWindow"> 
    <ScrollViewer> 
     <StackPanel> 
      <Menu Height="21" VerticalAlignment="Top"> 
       <MenuItem Header="File"/> 
       <MenuItem Header="Edit"/> 
      </Menu> 
      <StackPanel> 
       <TextBlock Text="1"/> 
       <TextBlock Text="2"/> 
       <TextBlock Text="3"/> 
       <TextBlock Text="4"/> 
       <TextBlock Text="5"/> 
       <TextBlock Text="6"/> 
       <TextBlock Text="7"/> 
       <TextBlock Text="8"/> 
       <TextBlock Text="9"/> 
       <TextBlock Text="10"/> 
      </StackPanel> 
     </StackPanel> 
    </ScrollViewer> 
</Window> 

然而,由於菜單是ScrollViewer中,菜單滾動出屏幕的一部分用戶向下滾動時。所以我把ScrollViewer只放在菜單下面的控件上:

<Window x:Class="WpfTest.MainWindow"   
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     Title="MainWindow" 
     Name="PrimaryWindow"> 
    <StackPanel> 
     <Menu Height="21" VerticalAlignment="Top"> 
      <MenuItem Header="File"/> 
      <MenuItem Header="Edit"/> 
     </Menu> 
     <ScrollViewer> 
      <StackPanel> 
       <TextBlock Text="1"/> 
       <TextBlock Text="2"/> 
       <TextBlock Text="3"/> 
       <TextBlock Text="4"/> 
       <TextBlock Text="5"/> 
       <TextBlock Text="6"/> 
       <TextBlock Text="7"/> 
       <TextBlock Text="8"/> 
       <TextBlock Text="9"/> 
       <TextBlock Text="10"/> 
      </StackPanel> 
     </ScrollViewer> 
    </StackPanel> 
</Window> 

但是這一次,ScrollViewer不起作用!即使我調整窗口的尺寸使其小於標籤所需的高度,滾動條也不會被激活。

我在做什麼錯?

回答

6

問題是由您的根StackPanel引起的,StackPanel不限制ScrollViewer的垂直高度。

嘗試使用DockPanel中來定位,而不是菜單:

<Window x:Class="WpfApplication1.Window1" 
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
Title="Window1" Height="300" Width="300"> 
<DockPanel> 
    <Menu DockPanel.Dock="Top" Height="21" VerticalAlignment="Top"> 
     <MenuItem Header="File"/> 
     <MenuItem Header="Edit"/> 
    </Menu> 
    <ScrollViewer> 
     <StackPanel> 
      <TextBlock Text="1"/> 
      <TextBlock Text="2"/> 
      <TextBlock Text="3"/> 
      <TextBlock Text="4"/> 
      <TextBlock Text="5"/> 
      <TextBlock Text="6"/> 
      <TextBlock Text="7"/> 
      <TextBlock Text="8"/> 
      <TextBlock Text="9"/> 
      <TextBlock Text="10"/> 
     </StackPanel> 
    </ScrollViewer> 
</DockPanel> 

+0

謝謝,工作! – HighCommander4

1

只有在祖先元素的高度或寬度改變時,ScrollViewer纔會出現它的條。所以,您的祖先是StackPanel,並且在您調整窗口大小時不會更改大小。

1

切勿使用一個StackPanel用的ScrollViewer裏面,因爲StackPanel的是作爲其內容想做大!所以ScrollViewer認爲它總是足夠的地方!

該scrollViewer必須在一切之外