2014-05-16 86 views
3

嘗試將滾動查看器添加到TextBlock,以便用戶可以向下滾動內容,這往往比可用的屏幕空間更長。對於什麼可能是一個愚蠢的問題表示抱歉:我可以看到有很多關於這個問題的主題,並且問題通常是固定的高度,但我很努力地看到哪些元素導致了我的XAML中的問題:WPF滾動查看器不能使用動態高度

<Popup StaysOpen="True" Placement="Center" IsOpen="{Binding SummaryOpen}" PlacementTarget="{Binding ElementName=Areas}"> 
    <Border Background="LightGray" BorderBrush="Black" Padding="5" BorderThickness="1"> 
     <Grid Width="500"> 
      <Grid.RowDefinitions> 
       <RowDefinition Height="50" /> 
       <RowDefinition Height="350" /> 
       <RowDefinition Height="40" /> 
      </Grid.RowDefinitions> 
      <StackPanel Grid.Row="0" Orientation="Horizontal"> 
       <Label Content="{Binding Name}" /> 
       <Label Content=": " /> 
       <Label Content="{Binding Description}" /> 
      </StackPanel> 
      <Border Grid.Row="1" BorderBrush="Black" BorderThickness="1"> 
       <StackPanel Background="White" Margin="-1,1,1,-1"> 
        <!-- this is the rogue element --> 
        <ScrollViewer VerticalScrollBarVisibility="Auto"> 
         <TextBlock Text="{Binding Summary}" TextWrapping="Wrap" /> 
        </ScrollViewer> 
       </StackPanel> 
      </Border> 
     </Grid> 
    </Border> 
</Popup> 

ScrollViewer出現,但從不包含實際的滾動條,無論TextBlock中有多少內容。

如果有人能解釋問題出在哪裏以及如何解決問題,我會非常感激。

回答

2

流氓元素實際上是父級StackPanel - 該面板本身不是「固定高度」,但它不作爲ScrollViewer的父級工作。原因是它將其可用高度報告爲無限大,所以子ScrollViewer認爲它可以延伸至其孩子需要的,因此不需要滾動。

它看起來像你可以很容易地使用一個邊界,或者電網,無論是其自己的高度將限制於母公司的高度,從而解決這個問題:

 <Border Grid.Row="1" BorderBrush="Black" BorderThickness="1"> 
      <Border Background="White" Margin="-1,1,1,-1"> 
       <!-- this is the rogue element --> 
       <ScrollViewer VerticalScrollBarVisibility="Auto"> 
        <TextBlock Text="{Binding Summary}" TextWrapping="Wrap" /> 
       </ScrollViewer> 
      </Border> 
     </Border> 
+0

啊,謝謝!我不知道StackPanels「默認」到無限高度,但感謝修復和有用的信息。 –