【问题标题】:Snapping ScrollViewer in Windows 8 Metro in Wide screens not snapping to the last item在宽屏幕中的 Windows 8 Metro 中捕捉 ScrollViewer 不会捕捉到最后一个项目
【发布时间】:2012-06-18 14:00:41
【问题描述】:

我已在 ScrollViewer 内的应用中启用捕捉点,如以下问题所述:Enabling ScrollViewer HorizontalSnapPoints with bindable collection

我遇到的问题是,当我在全高清显示器 (1920x1080) 中尝试我的应用程序时,每个项目的宽度都是 1400 像素。当我在项目#n-1 中捕捉到滚动时,我无法滚动到最后一个,因为它没有捕捉...

我不得不做的就是添加一个“假”项目,在最后是透明的,所以我可以滚动到我收藏的最后一个项目:

<Page.Resources>
    <Style x:Key="ItemsControlStyle" TargetType="ItemsControl">
    <Setter Property="ItemsPanel">
        <Setter.Value>
            <ItemsPanelTemplate>
                <VirtualizingStackPanel Orientation="Horizontal"/>
            </ItemsPanelTemplate>
        </Setter.Value>
    </Setter>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="ItemsControl">
                <ScrollViewer Style="{StaticResource HorizontalScrollViewerStyle}" HorizontalSnapPointsType="Mandatory" HorizontalSnapPointsAlignment="Near">
                    <ItemsPresenter />
                </ScrollViewer>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
    </Style>
</Page.Resources>

<ItemsControl Style="{StaticResource ItemsControlStyle}">
    <Border Background="Red" Height="1000" Width="1400"/>
    <Border Background="Blue" Height="1000" Width="1400"/>
    <Border Background="Green" Height="1000" Width="1400"/>
    <Border Background="Yellow" Height="1000" Width="1400"/>
    <Border Background="Magenta" Height="1000" Width="1400"/>
    <Border Background="Transparent" Height="1000" Width="1000" />
</ItemsControl>

我什至使用此 hack 时遇到的第二个问题是,我无法从 Metro 应用程序访问屏幕大小,因此我什至无法添加取决于屏幕的可变宽度的最后一项来解决这个问题。有什么建议吗?

谢谢!

【问题讨论】:

    标签: windows xaml microsoft-metro


    【解决方案1】:

    似乎以编程方式更改最后一项的宽度是最好的解决方案,使用 Window.Current.Bounds.Width;

    【讨论】:

    • 您如何找到更好的解决方案?当您的意思是更改宽度时,这实际上是否会使最后一项看起来比其他项小?
    【解决方案2】:

    我发现您可以使用 WindowSizeChanged 事件 (e.Size) 从 LayoutAwarePage.cs 中访问当前屏幕尺寸

    也就是说,我确信可能有更好的方法来访问此事件。

    【讨论】:

      【解决方案3】:

      之前提供的解决方案仅适用于少量案例(具有固定尺寸的物品)并且存在视觉问题。

      更新: 请参阅此处的示例Enabling ScrollViewer HorizontalSnapPoints with bindable collection

      不需要“假”项目,至于第二个:您不需要屏幕大小,只需要 ItemsPresenter.Parent.ActualHeight(或宽度,取决于使用的列表方向)和项目容器宽度 - 参见示例。

      【讨论】:

        【解决方案4】:

        ViewChanging 时修改 Horizo​​ntalSnapPointsAlignment,如下:

        private void sv_ViewChanging(object sender, ScrollViewerViewChangingEventArgs e)
            {
                if (e.NextView.HorizontalOffset <e.FinalView.HorizontalOffset)
                {
                    sv.HorizontalSnapPointsAlignment = SnapPointsAlignment.Far;
                }
                else if (e.NextView.HorizontalOffset > e.FinalView.HorizontalOffset)
                {
                    sv.HorizontalSnapPointsAlignment = SnapPointsAlignment.Near;
                }
            }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2012-08-24
          • 1970-01-01
          • 2016-05-28
          • 2018-03-12
          • 1970-01-01
          • 2019-03-22
          • 1970-01-01
          • 2014-02-09
          相关资源
          最近更新 更多