gpt4 book ai didi

c# - ScrollViewer 在 Canvas 中不起作用

转载 作者:行者123 更新时间:2023-11-30 23:21:30 27 4
gpt4 key购买 nike

我正在尝试在 Canvas 中使用 ScrollViewer,但滚动不起作用。

<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>

但是如果我用 Grid 切换 Canvas 一切正常。有没有办法让 ScrollViewerCanvas 中工作?

最佳答案

根据您在帖子中包含的代码,您似乎没有给 ScrollViewer 任何需要滚动的理由。 Canvas 元素不会以任何方式限制其子元素。所以在 Canvas 中,ScrollViewer 可以随心所欲地变大,因此它的大小足以容纳其子项而无需滚动。在 Grid 中,它会被拉伸(stretch)以适应其单元格,因此如果单元格小于子单元格,它将允许滚动。给它一个滚动的理由,它就会滚动。

例如,您可以使 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 的大小限制为小于其内容大小的东西都会导致滚动条变得可见和可用。

关于c# - ScrollViewer 在 Canvas 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39176629/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com