WP7 ScrollViewer 根本不工作

发布于 2024-12-22 16:10:56 字数 1887 浏览 4 评论 0原文

我的意图很简单。创建一种面板,用户可以在其中滚动浏览该特定面板中的许多控件。该面板内的控件可以是按钮、图像或标签……等等。

问题是...如果我将 ScrollViewer 设为垂直,它会滚动,但不会显示其内部的所有控件,而且,它不会停留在我滚动的位置。如果我让它水平,这就是我想要的,它根本不会滚动......一点也不滚动。

以下是我的代码:请帮忙!

<ScrollViewer Height="118" Name="scrollerButtons" Width="362" Canvas.Left="167" Canvas.Top="275" VerticalAlignment="Center">
        <StackPanel Height="97" Name="stackPanelButtons" Width="168" Orientation="Horizontal" Canvas.Left="162" Canvas.Top="43" VerticalAlignment="Center" HorizontalAlignment="Center">
                <Button Width="60" Height="60"> </Button>
                <Button Width="60" Height="60"></Button>
                <Button Width="60" Height="60"></Button>
                <Button Width="60" Height="60"></Button>
                <Button Width="60" Height="60"></Button>
                <Button Width="60" Height="60"></Button>
                <Button Width="60" Height="60"></Button>
                <Button Width="60" Height="60"></Button>
                <Button Width="60" Height="60"></Button>
                <Button Width="60" Height="60"></Button>
                <Button Width="60" Height="60"></Button>
                <Button Width="60" Height="60"></Button>
                <Button Width="60" Height="60"></Button>
                <Button Width="60" Height="60"></Button>
                <Button Width="60" Height="60"></Button>
                <Button Width="60" Height="60"></Button>
                <Button Width="60" Height="60"></Button>
                <Button Width="60" Height="60"></Button>
        </StackPanel>
        </ScrollViewer>

我添加了一堆按钮只是为了测试整个事情。非常感谢任何帮助。谢谢。

My intention is simple. Create a sort-of panel where the user can scroll through the many controls within that specific panel. The controls within that panel may be buttons, or images, or labels..whatever.

Thing is... If I make my ScrollViewer Vertical, it scrolls but it doesn't show all of the controls within itself, AND, it doesn't stay at the point where I scrolled it. And if I make it horizontal, which is how I want it to be, it doesn't scroll at all... Not one bit.

Below is my code: PLEASE HELP!

<ScrollViewer Height="118" Name="scrollerButtons" Width="362" Canvas.Left="167" Canvas.Top="275" VerticalAlignment="Center">
        <StackPanel Height="97" Name="stackPanelButtons" Width="168" Orientation="Horizontal" Canvas.Left="162" Canvas.Top="43" VerticalAlignment="Center" HorizontalAlignment="Center">
                <Button Width="60" Height="60"> </Button>
                <Button Width="60" Height="60"></Button>
                <Button Width="60" Height="60"></Button>
                <Button Width="60" Height="60"></Button>
                <Button Width="60" Height="60"></Button>
                <Button Width="60" Height="60"></Button>
                <Button Width="60" Height="60"></Button>
                <Button Width="60" Height="60"></Button>
                <Button Width="60" Height="60"></Button>
                <Button Width="60" Height="60"></Button>
                <Button Width="60" Height="60"></Button>
                <Button Width="60" Height="60"></Button>
                <Button Width="60" Height="60"></Button>
                <Button Width="60" Height="60"></Button>
                <Button Width="60" Height="60"></Button>
                <Button Width="60" Height="60"></Button>
                <Button Width="60" Height="60"></Button>
                <Button Width="60" Height="60"></Button>
        </StackPanel>
        </ScrollViewer>

I have added a bunch of buttons just to test the whole thing. Any help is HIGHLY appreciated. Thank you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

旧时浪漫 2024-12-29 16:10:57

我所做的是监听包含堆栈面板的网格上的 SizeChangedEvent,然后相应地调整 ScrollViewer 的高度。例如,在这里我只是将其设置为屏幕尺寸的一半。它并不完美,但它有效。

private void ContentPanel_SizeChanged(object sender, SizeChangedEventArgs e)
{
        // Resize the scroll view if the stackpanel is bigger, additional 70 for app bar
        if (e.NewSize.Height >
                (System.Windows.Application.Current.Host.Content.ActualHeight - 70))
        {
                ContentScrollViewer.VerticalScrollBarVisibility = ScrollBarVisibility.Visible;
                ContentScrollViewer.Height = System.Windows.Application.Current.Host.Content.ActualHeight / 2;
        }
}

What I did was listen to the SizeChangedEvent on a grid that contains the stack panel and then adjusted the height of the ScrollViewer accordingly. Here, for example I just make it half of the screen size. It is not perfect but it works.

private void ContentPanel_SizeChanged(object sender, SizeChangedEventArgs e)
{
        // Resize the scroll view if the stackpanel is bigger, additional 70 for app bar
        if (e.NewSize.Height >
                (System.Windows.Application.Current.Host.Content.ActualHeight - 70))
        {
                ContentScrollViewer.VerticalScrollBarVisibility = ScrollBarVisibility.Visible;
                ContentScrollViewer.Height = System.Windows.Application.Current.Host.Content.ActualHeight / 2;
        }
}
两相知 2024-12-29 16:10:56

从 StackPanel 中删除 Height 属性。您正在强制内部 StackPanel 截断其内容。由于内部 StackPanel (97) 比 ScrollViewer (118) 小,因此 ScrollViewer 无法滚动。 ScrollViewer 期望其内容比 ScrollViewer 本身大。

Remove the Height attribute from the StackPanel. You are forcing the inner StackPanel to truncate its content. Since the inner StackPanel is smaller (97) than the ScrollViewer (118), there is nothing for the ScrollViewer to scroll. The ScrollViewer expects its content to be larger than the ScrollViewer itself.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文