Exemplo n.º 1
0
        private async void GridViewPages_Loaded(object sender, RoutedEventArgs e)
        {
            await RunOnUIThreadAsync(CoreDispatcherPriority.High, () =>
            {
                if (GridViewPages.Items.Count >= 1)
                {
                    int index = ViewModel.SelectedPageIndex;

                    // fix GridView initially fails to select item by binding
                    GridViewPages.SelectedIndex = index;

                    // try scroll to selected item
                    try
                    {
                        GridViewItem item = (GridViewItem)GridViewPages.ContainerFromIndex(index);
                        Guard.IsNotNull(item, nameof(item));

                        BringIntoViewOptions options = new BringIntoViewOptions
                        {
                            AnimationDesired = false,
                        };
                        item.StartBringIntoView(options);
                    }
                    catch (Exception exc)
                    {
                        ViewModel.AppCenterService.TrackError(exc);
                    }
                }
            });
        }
Exemplo n.º 2
0
        private async void GridViewPages_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            await RunOnUIThreadAsync(CoreDispatcherPriority.Normal, () =>
            {
                if (GridViewPages.SelectionMode == ListViewSelectionMode.Single &&
                    e.AddedItems != null & e.AddedItems.Count != 0)
                {
                    // select page in view model
                    ViewModel.SelectedPageIndex = GridViewPages.SelectedIndex;

                    // scroll to newly selected item
                    try
                    {
                        GridViewItem item = (GridViewItem)GridViewPages.ContainerFromItem(e.AddedItems[0]);

                        BringIntoViewOptions options = new BringIntoViewOptions
                        {
                            AnimationDesired = true,
                        };
                        item.StartBringIntoView(options);
                    }
                    catch (Exception)
                    {
                    }
                }
                else if (GridViewPages.SelectionMode == ListViewSelectionMode.Single)
                {
                    try
                    {
                        GridViewPages.SelectedIndex = ViewModel.SelectedPageIndex;
                    }
                    catch (Exception) { }
                }
                else if (GridViewPages.SelectionMode != ListViewSelectionMode.Single)
                {
                    // connect items to view model
                    if (GridViewPages.SelectedRanges.Count == 0)
                    {
                        ViewModel.SelectedRanges = null;
                    }
                    else
                    {
                        ViewModel.SelectedRanges = GridViewPages.SelectedRanges;
                    }

                    // select last selected item in view model
                    if (e.AddedItems != null && e.AddedItems.Count != 0)
                    {
                        int index = GridViewPages.Items.IndexOf(e.AddedItems[e.AddedItems.Count - 1]);
                        ViewModel.SelectedPageIndex = index;
                    }
                }
            });
        }
        private void ListView_PreviewKeyDown(object sender, KeyRoutedEventArgs e)
        {
            list.UpdateLayout();

            if (listIndexsInEachRiver == null)
            {
                //Stores all gridviews(one row enabled)
                listRivers = TreeHelper.FindVisualChildren<GridView>(list);
                //Stores the selected index item of each river
                listIndexsInEachRiver = new List<int>();
                foreach (var item in listRivers)
                {
                    listIndexsInEachRiver.Add(item.SelectedIndex);
                }
            }

            //Declare:
            //Each river contains a title and a gridview(one row enabled). Remember it!!!

            //Current river
            var listItem = list.ContainerFromIndex(riverIndexInAllRivers) as ListViewItem;
            //Current GridView in current river
            var currentRiver = TreeHelper.FindVisualChild<GridView>(listItem);
            Debug.WriteLine("currentRiver.ActualWidth: " + currentRiver.ActualWidth);

            //Current item index in current river
            int itemIndexInCurrentRiver = currentRiver.SelectedIndex;

            //Current river index in all rivers
            //int riverIndexInCurrentRivers = listRivers.IndexOf(currentRiver);

            switch (e.Key)
            {
                case VirtualKey.Down:
                case VirtualKey.GamepadDPadDown:
                case VirtualKey.GamepadLeftThumbstickDown:
                    if (riverIndexInAllRivers + 1 < listRivers.Count)
                    {
                        var nextRiver = listRivers[riverIndexInAllRivers + 1];
                        nextRiver.Focus(FocusState.Programmatic);
                        if (nextRiver.Items != null && nextRiver.Items.Count > 0)
                        {
                            GridViewItem item = new GridViewItem();
                            if (listIndexsInEachRiver[riverIndexInAllRivers + 1] == -1)
                            {
                                item = nextRiver.ContainerFromIndex(0) as GridViewItem;
                                listIndexsInEachRiver[riverIndexInAllRivers + 1] = 0;
                            }
                            else
                            {
                                item = nextRiver.ContainerFromIndex(listIndexsInEachRiver[riverIndexInAllRivers + 1]) as GridViewItem;
                                listIndexsInEachRiver[riverIndexInAllRivers] = itemIndexInCurrentRiver;
                            }

                            item.Focus(FocusState.Programmatic);
                            item.IsSelected = true;
                            item.StartBringIntoView();
                        }

                        riverIndexInAllRivers++;
                    }

                    e.Handled = true;
                    break;
                case VirtualKey.Up:
                case VirtualKey.GamepadDPadUp:
                case VirtualKey.GamepadLeftThumbstickUp:
                    if (riverIndexInAllRivers - 1 >= 0)
                    {
                        var previousRiver = listRivers[riverIndexInAllRivers - 1];
                        previousRiver.Focus(FocusState.Programmatic);
                        if (previousRiver.Items != null && previousRiver.Items.Count > 0)
                        {
                            GridViewItem item = new GridViewItem();
                            if (listIndexsInEachRiver[riverIndexInAllRivers - 1] == -1) //Probably never happen
                            {
                                item = previousRiver.ContainerFromIndex(0) as GridViewItem;
                                listIndexsInEachRiver[riverIndexInAllRivers] = 0;
                            }
                            else
                            {
                                item = previousRiver.ContainerFromIndex(listIndexsInEachRiver[riverIndexInAllRivers - 1]) as GridViewItem;
                                listIndexsInEachRiver[riverIndexInAllRivers] = itemIndexInCurrentRiver;
                            }

                            item.Focus(FocusState.Programmatic);
                            item.IsSelected = true;
                            item.StartBringIntoView();
                        }

                        riverIndexInAllRivers--;
                    }

                    e.Handled = true;
                    break;
                case VirtualKey.Left:
                case VirtualKey.GamepadDPadLeft:
                case VirtualKey.GamepadLeftThumbstickLeft:
                    if (itemIndexInCurrentRiver - 1 >= 0)
                    {
                        currentRiver.Focus(FocusState.Programmatic);

                        GridViewItem item = new GridViewItem();
                        item = currentRiver.ContainerFromIndex(listIndexsInEachRiver[riverIndexInAllRivers]) as GridViewItem;
                        listIndexsInEachRiver[riverIndexInAllRivers] = itemIndexInCurrentRiver - 1;

                        item.Focus(FocusState.Programmatic);
                        item.IsSelected = true;
                        item.StartBringIntoView();

                        e.Handled = false;
                    }
                    else
                        e.Handled = true;
                    break;
                case VirtualKey.Right:
                case VirtualKey.GamepadDPadRight:
                case VirtualKey.GamepadLeftThumbstickRight:
                    if (itemIndexInCurrentRiver + 1 <= currentRiver.Items.Count - 1)
                    {
                        currentRiver.Focus(FocusState.Programmatic);

                        GridViewItem item = new GridViewItem();
                        item = currentRiver.ContainerFromIndex(listIndexsInEachRiver[riverIndexInAllRivers]) as GridViewItem;
                        listIndexsInEachRiver[riverIndexInAllRivers] = itemIndexInCurrentRiver + 1;

                        item.UpdateLayout();
                        item.Focus(FocusState.Programmatic);
                        item.IsSelected = true;
                        item.StartBringIntoView();

                        e.Handled = false;
                    }
                    else
                        e.Handled = true;
                    break;
                default:
                    break;
            }
        }