template item xaml listview windows-runtime windows-phone-8.1 scrollviewer

xaml - item - uwp datatemplate



La funciĆ³n Scrollviewer ChangeView no se desplaza hacia abajo (2)

He utilizado listview dentro de scrollviewer en Windows 8.1 de la aplicación del teléfono. Tengo un requisito de desplazamiento al final de la lista, pero la función de cambio de vista no funciona según los requisitos.

Creó una muestra para el mismo escenario:

MainPage.xaml

<Page x:Class="InfiList.MainPage" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="using:InfiList" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:customlv="using:InfiList" mc:Ignorable="d" Background="{ThemeResource ApplicationPageBackgroundThemeBrush}"> <Page.Resources> <Style x:Key="ListViewStyle1" TargetType="ListView"> <Setter Property="IsTabStop" Value="False"/> <Setter Property="TabNavigation" Value="Once"/> <Setter Property="IsSwipeEnabled" Value="True"/> <Setter Property="HorizontalContentAlignment" Value="Stretch"/> <Setter Property="VerticalContentAlignment" Value="Top"/> <Setter Property="ItemContainerTransitions"> <Setter.Value> <TransitionCollection> <AddDeleteThemeTransition/> <ReorderThemeTransition/> </TransitionCollection> </Setter.Value> </Setter> <Setter Property="ItemsPanel"> <Setter.Value> <ItemsPanelTemplate> <ItemsStackPanel Orientation="Vertical"/> </ItemsPanelTemplate> </Setter.Value> </Setter> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="ListView"> <Border BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}"> <ItemsPresenter FooterTransitions="{TemplateBinding FooterTransitions}" FooterTemplate="{TemplateBinding FooterTemplate}" Footer="{TemplateBinding Footer}" HeaderTemplate="{TemplateBinding HeaderTemplate}" Header="{TemplateBinding Header}" HeaderTransitions="{TemplateBinding HeaderTransitions}" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Padding="{TemplateBinding Padding}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/> </Border> </ControlTemplate> </Setter.Value> </Setter> </Style> </Page.Resources> <Grid x:Name="root"> <ScrollViewer x:Name="MyScrollViewer" ViewChanged="OnViewChanged" IsVerticalScrollChainingEnabled="True"> <ListView x:Name="listview" ItemsSource="{Binding Collection}" Style="{StaticResource ListViewStyle1}" > <ListView.ItemContainerStyle> <Style TargetType="ListViewItem"> <Setter Property="HorizontalContentAlignment" Value="Stretch"/> <Setter Property="VerticalContentAlignment" Value="Stretch"/> </Style> </ListView.ItemContainerStyle> <ListView.ItemsPanel> <ItemsPanelTemplate> <ItemsStackPanel ItemsUpdatingScrollMode="KeepItemsInView"/> </ItemsPanelTemplate> </ListView.ItemsPanel> <ListView.ItemTemplate> <DataTemplate> <Grid Background="LightGray" HorizontalAlignment="Left" Margin="25"> <TextBlock Margin="25 25 25 50" FontSize="32" Text="{Binding}" TextWrapping="WrapWholeWords" MaxWidth="200"/> </Grid> </DataTemplate> </ListView.ItemTemplate> </ListView> </ScrollViewer> </Grid> <Page.BottomAppBar> <CommandBar> <AppBarButton Click="AppBarButton_Click"> </AppBarButton> </CommandBar> </Page.BottomAppBar>

MainPage.xaml.cs

using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Diagnostics; using System.IO; using System.Linq; using System.Runtime.InteropServices.WindowsRuntime; using Windows.Foundation; using Windows.Foundation.Collections; using Windows.UI.Xaml; using Windows.UI.Xaml.Controls; using Windows.UI.Xaml.Controls.Primitives; using Windows.UI.Xaml.Data; using Windows.UI.Xaml.Input; using Windows.UI.Xaml.Media; using Windows.UI.Xaml.Navigation; // The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=391641 namespace InfiList { /// <summary> /// An empty page that can be used on its own or navigated to within a Frame. /// </summary> public sealed partial class MainPage : Page { public ObservableCollection<String> Collection { get; set; } private bool incall; private int offset; int _noofelements; public MainPage() { this.InitializeComponent(); Collection = new ObservableCollection<string>(); listview.DataContext = this; this.NavigationCacheMode = NavigationCacheMode.Required; addNumber(0); listview.Loaded += listview_Loaded; } void listview_Loaded(object sender, RoutedEventArgs e) { MyScrollViewer.ChangeView(null, int.MaxValue, null, true); } private void OnViewChanged(object sender, ScrollViewerViewChangedEventArgs e) { ScrollViewer view = (ScrollViewer)sender; Debug.WriteLine("Vertica Pffset: " + view.VerticalOffset); if ((view.VerticalOffset < 0.1 * view.ScrollableHeight) & !incall) { incall = true; addNumber(++offset); } } private void addNumber(int offset) { int scrollcount = 30; int start = offset * scrollcount; for (int i = start; i < start + scrollcount; i++) { string s= (_noofelements++).ToString() ; if (i % 2 == 0) s += "msdkd kmsdksdk kdsmkd skmcds ckdsmckds ckdsmcksd kcmdskcdsc kdmmcsckdsc" + Environment.NewLine + "sdjndsjnds" + "msdkd kmsdksdk kdsmkd skmcds ckdsmckds ckdsmcksd kcmdskcdsc kdmmcsckdsc" + Environment.NewLine + "sdjndsjnds" + "dssdsddsds"; Collection.Insert(0,s); } incall = false; } /// <summary> /// Scrolls to bottom /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void AppBarButton_Click(object sender, RoutedEventArgs e) { MyScrollViewer.ChangeView(null, int.MaxValue, null, true); //MyScrollViewer.ScrollToVerticalOffset(int.MaxValue); //For scrolling till top //MyScrollViewer.ScrollToVerticalOffset(0); } } }

¿Hay alguna forma de lograr la funcionalidad deseada?



Intente utilizar ListView.ScrollIntoView , por ejemplo:

listView.ScrollIntoView (listView.Items.Last (), ScrollIntoViewAlignment.Leading);