在Windows 8 Metro应用程序中,是否可以创建一个ScrollViewer,在到达视图中的最后一项时,将其循环回第一项?如果可以,如何实现这个效果?
</UserControl.Resources>
<Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}">
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<ScrollViewer x:Name="ScrollViewer1">
<ListBox x:Name="SampleListBox" Background="White" ItemsSource="{Binding Path=sampleItems}" ItemTemplate="{StaticResource sampleTemplate}" VerticalAlignment="Stretch" HorizontalAlignment="Stretch" Grid.Row="0" Grid.RowSpan="2">
</ListBox>
</ScrollViewer>
</Grid>
public sealed partial class MainPage : Page
{
List<SampleItem> sampleItems;
const int numItems = 15;
public MainPage()
{
sampleItems = new List<SampleItem>();
for (int i = 0; i < numItems; i++)
{
sampleItems.Add(new SampleItem(i));
}
this.InitializeComponent();
SampleListBox.ItemsSource = sampleItems;
ScrollViewer1.ViewChanged += ScrollViewer1_ViewChanged;
}
void ScrollViewer1_ViewChanged(object sender, ScrollViewerViewChangedEventArgs e)
{
ScrollViewer viewer = sender as ScrollViewer;
ListBox box = viewer.Content as ListBox;
ListBoxItem lbi = box.ItemContainerGenerator.ContainerFromIndex(0) as ListBoxItem;
double elementSize;
if (lbi == null)
return;
elementSize = lbi.ActualHeight;
} /// <summary>
/// Invoked when this page is about to be displayed in a Frame.
/// </summary>
/// <param name="e">Event data that describes how this page was reached. The Parameter
/// property is typically used to configure the page.</param>
protected override void OnNavigatedTo(NavigationEventArgs e)
{
}
}
public class SampleItem
{
public String ItemCount { get; set; }
public SampleItem(int itemCount)
{
ItemCount = itemCount.ToString();
}
}
我不相信在WinRT/XAML中有像那样的控件,所以您需要实现一个自定义控件。有很多方法可以采取,但我可能会避免使用ScrollViewer,并直接处理操作事件,因为将ScrollViewer的行为弯曲到您的要求可能不容易。 我将基于操作事件控制滚动偏移量,并基于滚动偏移量 - 定位视图中的元素 - 例如使用Canvas控件。您需要根据滚动偏移量重新定位项目面板中的元素,以便例如超出视口一端的项目被移到另一端。这将涉及自定义依赖属性、项容器等。如果您熟悉所有这些API,则可能需要至少几个小时的工作。