2016-08-27 51 views
1

我試圖在Canvas內使用ScrollViewer,但滾動不起作用。ScrollViewer無法在Canvas中工作

<Page 
    x:Class="ScrollViewerInCanvas.MainPage" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:local="using:ScrollViewerInCanvas" 
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
    mc:Ignorable="d"> 

    <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}"> 
    <Canvas> 
     <ScrollViewer> 
     <StackPanel Orientation="Vertical" Width="400"> 
      <TextBlock Text="Just a huge text that will not fit into a single frame" 
        FontSize="100" TextWrapping="WrapWholeWords" /> 
     </StackPanel> 
     </ScrollViewer> 
    </Canvas> 
    </Grid> 
</Page> 

但是如果我切換CanvasGrid一切正常。有沒有辦法讓ScrollViewerCanvas裏面工作?

+0

@PeterDuniho這實際上是最小的例子,再現我的問題。我對Canvas如何工作的誤解是問題的根源。你對我的問題的回答實際上正是我所需要的。請移動你的部分答案,你描述了Canvas如何工作到「答案」,我會將其標記爲我的問題的答案。 – Geslot

+0

好的,完成了。請注意我編輯到您的問題中的代碼示例。這是預期的那種代碼示例,以確保對問題的容易理解和可重複性。有關更多詳細信息,請參閱[mcve](包括該文末尾的鏈接)。 –

回答

1

根據您在帖子中包含的代碼,您看不到ScrollViewer需要滾動的任何理由。 A Canvas元素不會以任何方式限制其子元素。因此,在Canvas中,ScrollViewer可以大到想要的大小,所以它會足夠大以容納其子級而不滾動。在Grid中,它將被拉伸以適應其細胞,因此如果細胞比孩子小,它將允許滾動。給它一個滾動的理由,它會。

例如,你可以使ScrollViewer始終是相同的尺寸,其Canvas父:

<Page 
    x:Class="ScrollViewerInCanvas.MainPage" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:local="using:ScrollViewerInCanvas" 
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
    mc:Ignorable="d"> 

    <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}"> 
    <Canvas x:Name="canvas1"> 
     <ScrollViewer Width="{Binding ActualWidth, ElementName=canvas1}" 
        Height="{Binding ActualHeight, ElementName=canvas1}"> 
     <StackPanel Orientation="Vertical" Width="400"> 
      <TextBlock Text="Just a huge text that will not fit into a single frame" 
        FontSize="100" TextWrapping="WrapWholeWords" /> 
     </StackPanel> 
     </ScrollViewer> 
    </Canvas> 
    </Grid> 
</Page> 

任何會限制了ScrollViewer的大小的東西比它的內容的尺寸會導致滾動條變得可見和可用。

+0

謝謝,這正是我需要的! – Geslot

相關問題