private void ListBox_MouseDown(object sender, MouseEventArgs e) { if (e.Button == MouseButtons.Right) { ListBox lb = (ListBox)sender; var index = lb.IndexFromPoint(e.Location); if (index != ListBox.NoMatches) { lb.SelectedIndex = index; _selectedMenuItem = lb.Items[index].ToString(); // Show different context menus for each ListBox switch (lb.Name) { case "ListBoxIP": ListBoxIPMenu.Show(Cursor.Position); ListBoxIPMenu.Visible = true; break; default: ListBoxMenu.Show(Cursor.Position); ListBoxMenu.Visible = true; break; } } else { ListBoxMenu.Visible = false; } } }
private void ExecuteGotoPage(string navViewName) { try { var targetItem = ListBoxMenu.FindChildren <UserControl>().FirstOrDefault(uc => uc.GetType().Name == navViewName); if (targetItem != null) { var index = ListBoxMenu.Items.IndexOf(targetItem); ListBoxItem listBoxItem = ListBoxMenu.ItemContainerGenerator.ContainerFromIndex(index) as ListBoxItem; if (listBoxItem != null) { listBoxItem.IsSelected = true; listBoxItem.Focus(); } } } catch (Exception ex) { Log.Logger.Error($"ExecuteGotoPage => {ex}"); } }
/// <summary> /// Metoda filtrująca nasze elementy w menu /// </summary> /// <param name="categoryId">według jakiej kategorii sortować</param> /// <param name="onlyActive">czy pokazać tylko aktywne elementy menu</param> private void HideInactiveItems(int categoryId, bool onlyActive = false) { ListBoxMenu.UpdateLayout(); foreach (MenuItem item in ListBoxMenu.ItemsSource) { if (ListBoxMenu.ItemContainerGenerator.ContainerFromItem(item) is ListBoxItem listBoxItem) { if ((!item.IsActive && onlyActive) || item.CategoryId != categoryId) { listBoxItem.Visibility = Visibility.Collapsed; } else { listBoxItem.Visibility = Visibility.Visible; } } } }