gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 23:46:31 27 4
gpt4 key购买 nike

我正在尝试创建与 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/

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