|
| 1 | +using System; |
| 2 | +using System.Collections; |
| 3 | +using System.Collections.Generic; |
| 4 | +using System.Linq; |
| 5 | +using System.Text; |
| 6 | +using System.Threading.Tasks; |
| 7 | +using Microsoft.UI.Xaml; |
| 8 | +using Microsoft.UI.Xaml.Controls; |
| 9 | +using Microsoft.UI.Xaml.Media; |
| 10 | + |
| 11 | +namespace WinUIGallery.Helper |
| 12 | +{ |
| 13 | + public static class TabViewHelper |
| 14 | + { |
| 15 | + public static void PopulateTabViewContextMenu(MenuFlyout contextMenu) |
| 16 | + { |
| 17 | + contextMenu.Items.Clear(); |
| 18 | + |
| 19 | + var item = (TabViewItem)contextMenu.Target; |
| 20 | + ListView tabViewListView = null; |
| 21 | + TabView tabView = null; |
| 22 | + |
| 23 | + DependencyObject current = item; |
| 24 | + |
| 25 | + while (current != null) |
| 26 | + { |
| 27 | + DependencyObject parent = VisualTreeHelper.GetParent(current); |
| 28 | + |
| 29 | + if (parent is ListView parentTabViewListView) |
| 30 | + { |
| 31 | + tabViewListView = parentTabViewListView; |
| 32 | + } |
| 33 | + else if (parent is TabView parentTabView) |
| 34 | + { |
| 35 | + tabView = parentTabView; |
| 36 | + } |
| 37 | + |
| 38 | + if (tabViewListView != null && tabView != null) |
| 39 | + { |
| 40 | + break; |
| 41 | + } |
| 42 | + |
| 43 | + current = parent; |
| 44 | + } |
| 45 | + |
| 46 | + if (tabViewListView == null || tabView == null) |
| 47 | + { |
| 48 | + return; |
| 49 | + } |
| 50 | + |
| 51 | + // First, if there are tabs to the left or to the right of the tab on which this context menu is opening, |
| 52 | + // then we'll include menu items to move this tab to the left or to the right. |
| 53 | + // |
| 54 | + // There are two possible cases for tab views: either they have explicitly set tab items, or they have a data item source set. |
| 55 | + // To move a tab left or right with explicitly set tab items, we'll remove and replace the tab item itself. |
| 56 | + // To move a tab left or right with a data item source set, we'll instead remove and replace the data item in the source list. |
| 57 | + int index = tabViewListView.IndexFromContainer(item); |
| 58 | + |
| 59 | + if (index > 0) |
| 60 | + { |
| 61 | + MenuFlyoutItem moveLeftItem = new() { Text = "Move tab left" }; |
| 62 | + moveLeftItem.Click += (s, args) => |
| 63 | + { |
| 64 | + if (tabView.TabItemsSource is IList itemsSourceList) |
| 65 | + { |
| 66 | + var item = itemsSourceList[index]; |
| 67 | + itemsSourceList.RemoveAt(index); |
| 68 | + itemsSourceList.Insert(index - 1, item); |
| 69 | + } |
| 70 | + else |
| 71 | + { |
| 72 | + var item = tabView.TabItems[index]; |
| 73 | + tabView.TabItems.RemoveAt(index); |
| 74 | + tabView.TabItems.Insert(index - 1, item); |
| 75 | + } |
| 76 | + }; |
| 77 | + contextMenu.Items.Add(moveLeftItem); |
| 78 | + } |
| 79 | + |
| 80 | + if (index < tabViewListView.Items.Count - 1) |
| 81 | + { |
| 82 | + MenuFlyoutItem moveRightItem = new() { Text = "Move tab right" }; |
| 83 | + moveRightItem.Click += (s, args) => |
| 84 | + { |
| 85 | + if (tabView.TabItemsSource is IList itemsSourceList) |
| 86 | + { |
| 87 | + var item = itemsSourceList[index]; |
| 88 | + itemsSourceList.RemoveAt(index); |
| 89 | + itemsSourceList.Insert(index + 1, item); |
| 90 | + } |
| 91 | + else |
| 92 | + { |
| 93 | + var item = tabView.TabItems[index]; |
| 94 | + tabView.TabItems.RemoveAt(index); |
| 95 | + tabView.TabItems.Insert(index + 1, item); |
| 96 | + } |
| 97 | + }; |
| 98 | + contextMenu.Items.Add(moveRightItem); |
| 99 | + } |
| 100 | + } |
| 101 | + } |
| 102 | +} |
0 commit comments