none
如何得知GridView控件的所有Item全部加载完毕 RRS feed

  • 问题

  • 大家好:

    metro中如何判断一个gridview中所有的item都加载完毕了?以便将视图滚动到选中的Item上?

    2013年9月23日 14:46

答案

  • Hi,

    GridviewLoaded事件触发的话表示GridView中所有的item都加载完毕了。您是指您在后台选中了一个item,然后scrollviewer会移动相对应的位置吗?如果是这样子的话,您可以通过VisualTreeHelper来得到GridView里面的Scrollviewer,然后用ScrollViewer.ScrollToVerticalOffset或者ScrollViewer.ScrollToHorizontalOffset方法来设置将ScrollViewer 内的内容滚动到指定的垂直或者水平偏移量位置。

    以下是一个例子您可以参考:

    XAML:

    <Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}" Name="grid"> <GridView ItemsSource="{Binding}" Name="gridview" Loaded="gridview_Loaded" Width="300" Height="100" ScrollViewer.IsHorizontalRailEnabled="True" ScrollViewer.HorizontalScrollBarVisibility="Visible"> <GridView.ItemTemplate> <DataTemplate> <StackPanel> <Image Source="{Binding Image}" Width="100" Height="100"/> </StackPanel> </DataTemplate> </GridView.ItemTemplate> </GridView> </Grid>

    Code-Behind:

    public sealed partial class MainPage : Page
        {
            public MainPage()
            {
                this.InitializeComponent();
                List<tt> TT = new List<tt>();
                TT.Add(new tt("Assets/Logo.png"));
                TT.Add(new tt("Assets/Logo.png"));
                TT.Add(new tt("Assets/Logo.png"));
                TT.Add(new tt("Assets/Logo.png"));
                grid.DataContext = TT;
            }
    
            /// <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)
            {
                
                
            }
            private void vtree(DependencyObject obj, int level)
            {
                
                for (int i = 0; i < VisualTreeHelper.GetChildrenCount(obj); ++i)
                {
                    vtree(VisualTreeHelper.GetChild(obj, i), ++level);
                    if(VisualTreeHelper.GetChild(obj,i).GetType().Equals(typeof(ScrollViewer)))
                    {
                        ScrollViewer scoller = VisualTreeHelper.GetChild(obj, i) as ScrollViewer;
                        scoller.ScrollToHorizontalOffset(gridview.SelectedIndex+1);
                    }
                }
            }
            private void gridview_Loaded(object sender, RoutedEventArgs e)
            {
                gridview.SelectedIndex = 3;
                DependencyObject deObj = sender as DependencyObject;
                vtree(deObj, 0);
               
            }
            
        }
        public class tt
        {
            public tt( string image)
            {
                this.Image = image;
            }
            public string Image
            {
                get;
                set;
            }
        
        }
    }

    或者您还可以通过一个更简单的方法,用Gridview自带的ScrollIntoView(Object, ScrollIntoViewAlignment) 方法,代码如下:

    private void gridview_Loaded(object sender, RoutedEventArgs e)
            {
                gridview.SelectedIndex = 3;
                gridview.ScrollIntoView(gridview.SelectedItem, ScrollIntoViewAlignment.Default);
               
            }

    祝您好运!



    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.


    2013年9月24日 9:09
    版主