/// <summary> /// Manage the sort of the items /// </summary> private void ItemsDataGrid_Sorting(object sender, DataGridSortingEventArgs e) { if (e.Column.SortDirection == null || e.Column.SortDirection == ListSortDirection.Descending) { // Sort by asc ItemsDataGrid.ItemsSource = EncycloDB.SortPreviousSearch(e.Column.SortMemberPath, ListSortDirection.Ascending); ItemsDataGrid.Columns[e.Column.DisplayIndex].SortDirection = ListSortDirection.Descending; // Inversed sort because the event will automatically inverse it again ActualSortDirection = ListSortDirection.Ascending; } else { // Sort by desc ItemsDataGrid.ItemsSource = EncycloDB.SortPreviousSearch(e.Column.SortMemberPath, ListSortDirection.Descending); ItemsDataGrid.Columns[e.Column.DisplayIndex].SortDirection = ListSortDirection.Ascending; // Inversed sort because the event will automatically inverse it again ActualSortDirection = ListSortDirection.Descending; } ActualSortedColumnIndex = e.Column.DisplayIndex; // Scroll to top after the sorting // source: https://social.msdn.microsoft.com/Forums/en-US/fdccbf75-bd1c-41c6-b209-71d6537603df/datagrid-event-after-columns-sorting Dispatcher.BeginInvoke((Action) delegate() { if (ItemsDataGrid.Items.Count > 0) { ItemsDataGrid.ScrollIntoView(ItemsDataGrid.Items[0]); } }, null); }
/// <summary> /// Load the differents data to the form /// </summary> private void FrmMain_Loaded(object sender, RoutedEventArgs e) { try { // Load the actual build to the UserControls UcActualBuildStats.UcBuild = ActualBuild; UcActualCustomBuildStats.UcBuild = ActualBuild; UcActualSkillsManager.ActualSkills = ActualBuild.BSkill; UcActualRunesManager.ActualBuild = ActualBuild; // Load data in datagrid and combobox ItemsDataGrid.ItemsSource = EncycloDB.GetAllItemsWithImg(); MscbxRarity.ItemsSource = EncycloDB.GetAllRarities(); MscbxType.ItemsSource = EncycloDB.GetAllTypes(); MscbxStats.ItemsSource = EncycloDB.GetAllStats(); // Create events CreateBuildImagesEvents(); // Create an event that will trigger when the data of the datagrid are updated CollectionView myCollectionView = (CollectionView)CollectionViewSource.GetDefaultView(ItemsDataGrid.Items); ((INotifyCollectionChanged)myCollectionView).CollectionChanged += ItemsDataGrid_CollectionChanged; } catch (Exception ex) { MessageBox.Show(ex.Message); } }
/// <summary> /// Lazy loading of items /// </summary> private void ItemsDataGrid_ScrollChanged(object sender, ScrollChangedEventArgs e) { ScrollViewer scrollViewer = (ScrollViewer)e.OriginalSource; if (ChangeVerticalOffset) { scrollViewer.ScrollToVerticalOffset(PreviousScrollVerticalOffset); ChangeVerticalOffset = false; } else { PreviousScrollVerticalOffset = e.VerticalOffset; // Trigger when the scrollbar is at the end if (e.VerticalOffset == scrollViewer.ScrollableHeight && e.VerticalOffset != 0 && !IsScrollAlreadyAtTheEnd) { ItemsDataGrid.ItemsSource = EncycloDB.LoadNewItems(); if (ActualSortedColumnIndex != 0) { ItemsDataGrid.Columns[ActualSortedColumnIndex].SortDirection = ActualSortDirection; } IsScrollAlreadyAtTheEnd = true; ChangeVerticalOffset = true; } else { IsScrollAlreadyAtTheEnd = false; } } }
/// <summary> /// Search the items by differents filter /// </summary> private void BtnSearchItems_Click(object sender, RoutedEventArgs e) { // Force the correction of the levels before searching the items TbxItemLvlSearch_LostFocus(TbxItemLvlMinSearch, e); TbxItemLvlSearch_LostFocus(TbxItemLvlMaxSearch, e); int minLvl = Convert.ToInt32(TbxItemLvlMinSearch.Text); int maxLvl = Convert.ToInt32(TbxItemLvlMaxSearch.Text); string nameItem = TbxNameSearch.Text.Trim(); int[] idRarities = GetIntArrayOfSelectedElements(MscbxRarity.GetListElements()); int[] idTypes = GetIntArrayOfSelectedElements(MscbxType.GetListElements()); int[] idStats = GetIntArrayOfSelectedElements(MscbxStats.GetListElements()); ItemsDataGrid.ItemsSource = EncycloDB.SearchItems(minLvl, maxLvl, nameItem, idRarities, idTypes, idStats); ActualSortedColumnIndex = 0; // Reset the sort if (ItemsDataGrid.Items.Count > 0) { ItemsDataGrid.ScrollIntoView(ItemsDataGrid.Items[0]); } CloseAllPopup(); }
/// <summary> /// Show next items of the datagrid /// </summary> private void BtnShowNextItems_Click(object sender, RoutedEventArgs e) { ItemsDataGrid.ItemsSource = EncycloDB.GoToNextItems(); }