xaml - 使用可绑定(bind)集合启用 ScrollViewer Horizo​​ntalSnapPoints

标签 xaml windows-8 winrt-xaml

我正在尝试创建与 Windows 8 SDK 示例中的 ScrollViewerSample 类似的体验,以便能够在左右滚动时捕捉到 ScrollViewer 内的项目。示例中的实现(有效)如下所示:

<ScrollViewer x:Name="scrollViewer" Width="480" Height="270"
              HorizontalAlignment="Left" VerticalAlignment="Top"
              VerticalScrollBarVisibility="Disabled" HorizontalScrollBarVisibility="Auto" 
              ZoomMode="Disabled" HorizontalSnapPointsType="Mandatory">
    <StackPanel Orientation="Horizontal">
        <Image Width="480" Height="270" AutomationProperties.Name="Image of a cliff" Source="images/cliff.jpg" Stretch="None"  HorizontalAlignment="Left" VerticalAlignment="Top"/>
        <Image Width="480" Height="270" AutomationProperties.Name="Image of Grapes" Source="images/grapes.jpg" Stretch="None" HorizontalAlignment="Left" VerticalAlignment="Top"/>
        <Image Width="480" Height="270" AutomationProperties.Name="Image of Mount Rainier" Source="images/Rainier.jpg" Stretch="None" HorizontalAlignment="Left" VerticalAlignment="Top"/>
        <Image Width="480" Height="270" AutomationProperties.Name="Image of a sunset" Source="images/sunset.jpg" Stretch="None" HorizontalAlignment="Left" VerticalAlignment="Top"/>
        <Image Width="480" Height="270" AutomationProperties.Name="Image of a valley" Source="images/valley.jpg" Stretch="None" HorizontalAlignment="Left" VerticalAlignment="Top"/>
    </StackPanel>
</ScrollViewer>

与我想要的实现的唯一区别是我不想要一个里面有项目的 StackPanel,而是我可以绑定(bind)到的东西。我正在尝试使用 ItemsControl 来完成此操作,但由于某种原因,Snap 行为没有生效:
<ScrollViewer x:Name="scrollViewer" Width="480" Height="270"
              HorizontalAlignment="Left" VerticalAlignment="Top"
              VerticalScrollBarVisibility="Disabled" HorizontalScrollBarVisibility="Auto" 
              ZoomMode="Disabled" HorizontalSnapPointsType="Mandatory">
    <ItemsControl>
        <ItemsControl.ItemsPanel>
            <ItemsPanelTemplate>
                <StackPanel Orientation="Horizontal" />
            </ItemsPanelTemplate>
        </ItemsControl.ItemsPanel>
        <Image Width="480" Height="270" AutomationProperties.Name="Image of a cliff" Source="images/cliff.jpg" Stretch="None"  HorizontalAlignment="Left" VerticalAlignment="Top"/>
        <Image Width="480" Height="270" AutomationProperties.Name="Image of Grapes" Source="images/grapes.jpg" Stretch="None" HorizontalAlignment="Left" VerticalAlignment="Top"/>
        <Image Width="480" Height="270" AutomationProperties.Name="Image of Mount Rainier" Source="images/Rainier.jpg" Stretch="None" HorizontalAlignment="Left" VerticalAlignment="Top"/>
        <Image Width="480" Height="270" AutomationProperties.Name="Image of a sunset" Source="images/sunset.jpg" Stretch="None" HorizontalAlignment="Left" VerticalAlignment="Top"/>
        <Image Width="480" Height="270" AutomationProperties.Name="Image of a valley" Source="images/valley.jpg" Stretch="None" HorizontalAlignment="Left" VerticalAlignment="Top"/>
    </ItemsControl>
</ScrollViewer>

建议将不胜感激!

感谢 Denis,我最终在 ItemsControl 上使用了以下样式,并完全删除了 ScrollViewer 和内联 ItemsPanelTemplate:
<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">
                    <ItemsPresenter />
                </ScrollViewer>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

最佳答案

让快照点为绑定(bind)集合工作可能很棘手。要使 ScrollViewer 的直接子节点工作的快照点应该实现 IScrollSnapPointsInfo 接口(interface)。 ItemsControl 没有实现 IScrollSnapPointsInfo,因此您不会看到捕捉行为。

要解决此问题,您有几个选择:

  • 创建从 ItemsControl 派生的自定义类并实现 IScrollSnapPointsInfo 接口(interface)。
  • 为项目控件创建自定义样式并在样式内的 ScrollViewer 上设置 Horizo​​ntalSnapPointsType 属性。

  • 我已经实现了以前的方法,并且可以确认它有效,但在您的情况下,自定义样式可能是更好的选择。

    关于xaml - 使用可绑定(bind)集合启用 ScrollViewer Horizo​​ntalSnapPoints,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10107470/

    相关文章:

    .net - 如何在运行时动态更改 MultiBinding(或任何其他)资源的 ConverterParameter(或任何)属性?

    c# - 在 xamarin 表单中的 ScrollView 中时, ListView 滚动问题

    windows-8 - 是否可以在 Windows 8 上模拟 ARM?

    c# - Windows Phone 8.1 尺寸问题

    c# - 使用 C# 和 XAML 在 Metro 应用程序中自动滚动

    Windows 8 上的 XAML 转换已忽略

    c# - Metro/WinRT/Windows 8 是否可以从后面的代码中清除元素的绑定(bind)?

    windows-8 - 有没有办法在 Windows 8 metro 应用程序中获得推送滚动功能?

    winapi - Golang : using Windows 10 API/UWP/System. Windows 运行时?

    xaml - Win 8.1 metro 枢纽导航