public void addAllItems(ListBox src, ListBox target, bool IsReset = false) { try { if (src == null || target == null) return; if (IsReset == true) { target.Items.Clear(); } src.UnselectAll(); src.SelectAll(); addItems(src, target); if (IsReset == true) { src.UnselectAll(); target.UnselectAll(); } } catch (Exception ex) { MessageBox.Show(ex.Message + "\r\n" + ex.StackTrace); } }
/// <summary> /// Builds a tab item for a detection method /// </summary> /// <param name="method">The detection method to build it from</param> /// <returns>The constructed tab item</returns> private TabItem GenerateTabItemFromDetectionMethod(IDetectionMethod method) { var tabItem = new TabItem { Header = new TextBlock { Text = method.Abbreviation }, IsEnabled = FeaturesEnabled }; var tabItemGrid = new Grid(); tabItemGrid.RowDefinitions.Add(new RowDefinition { Height = new GridLength(1, GridUnitType.Auto) }); tabItemGrid.RowDefinitions.Add(new RowDefinition { Height = new GridLength(1, GridUnitType.Auto) }); tabItemGrid.RowDefinitions.Add(new RowDefinition { Height = new GridLength(1, GridUnitType.Star) }); var title = new TextBlock { Text = method.Name, FontWeight = FontWeights.Bold, FontSize = 16, Margin = new Thickness(3), TextWrapping = TextWrapping.Wrap }; Grid.SetRow(title, 0); tabItemGrid.Children.Add(title); var aboutButton = new Button { Content = new Image { Source = new BitmapImage(new Uri("pack://application:,,,/Images/help_32.png", UriKind.Absolute)), Width = 16, Height = 16 }, Margin = new Thickness(3), HorizontalAlignment = HorizontalAlignment.Right, HorizontalContentAlignment = HorizontalAlignment.Center }; aboutButton.Click += (o, e) => Common.ShowMessageBox(string.Format(" About {0} ", method.Name), method.About, false, false); Grid.SetRow(aboutButton, 0); tabItemGrid.Children.Add(aboutButton); var detectionMethodOptions = new GroupBox { Header = new TextBlock { Text = "Options" }, BorderBrush = Brushes.OrangeRed }; var optionsStackPanel = new StackPanel { Orientation = Orientation.Vertical }; detectionMethodOptions.Content = optionsStackPanel; var detectionMethodListBox = new GroupBox { Header = new TextBlock { Text = "Detected Values" }, BorderBrush = Brushes.OrangeRed }; var settingsGrid = method.SettingsGrid; var listBox = new ListBox { SelectionMode = SelectionMode.Extended, IsEnabled = method.IsEnabled }; settingsGrid.IsEnabled = method.IsEnabled; optionsStackPanel.Children.Add(settingsGrid); Grid.SetRow(detectionMethodOptions, 1); if (method.HasSettings) tabItemGrid.Children.Add(detectionMethodOptions); method.ListBox = listBox; detectionMethodListBox.Content = listBox; listBox.SelectionChanged += (o, e) => { if (e.AddedItems.Count > 0) _selectionBehaviour.ResetSelection(); }; _selectionBehaviour.SelectionMade += (o, e) => listBox.UnselectAll(); Grid.SetRow(detectionMethodListBox, 2); tabItemGrid.Children.Add(detectionMethodListBox); listBox.SelectionChanged += (o, e) => { var box = o as ListBox; if (box != null) ActionsEnabled = Selection != null || box.SelectedItems.Count > 0; else ActionsEnabled = Selection != null; }; tabItem.Content = tabItemGrid; return tabItem; }
private KeyEventHandler getBoxKeyEnableHandler(ListBox box, KeyEventHandler handler, bool src) { return new KeyEventHandler((sender, e) => { if (Keyboard.Modifiers == ModifierKeys.None) { switch (e.Key) { case Key.Escape: if (box.SelectedItem != null) { box.UnselectAll(); e.Handled = true; } break; case Key.Enter: if (src == true) { handler(sender, e); //一つ下へ移動する。ただし、カーソル位置は正しく動かない。 int pos = box.SelectedIndex + 1; box.SelectedIndex = Math.Max(0, Math.Min(pos, box.Items.Count - 1)); box.ScrollIntoViewFix(box.SelectedIndex); e.Handled = true; } break; case Key.Delete: if (src == false) { handler(sender, e); e.Handled = true; } break; } } }); }
private void allowCancelAction(ListBox box) { if (box == null) return; // box.MouseLeftButtonUp += new MouseButtonEventHandler((sender, e) => { if (box.GetPlacementItem() != null) return; // box.UnselectAll(); }); box.KeyDown += new KeyEventHandler((sender, e) => { if (Keyboard.Modifiers != ModifierKeys.None) return; // switch (e.Key) { case Key.Escape: if (box.SelectedIndex >= 0) { box.UnselectAll(); e.Handled = true; } break; } }); }
/// <summary>アイテムを上下に一つ移動</summary> public bool bxMoveItems(ListBox box, int direction, IList boxItemsSource = null) { try { if (box == null || box.SelectedIndex < 0) return false; var boxItems = boxItemsSource ?? box.Items; var selected = box.SelectedItemsList();//連続移動の視点固定のため順番保持 int iCount = boxItems.Count;//固定 var r = direction >= 0 ? (Func<int, int>)(i => iCount - 1 - i) : (i => i); for (int i = 0; i < boxItems.Count; i++) { var item = boxItems[r(i)]; if (box.SelectedItems.Contains(item) == true) { boxItems.RemoveAt(r(i)); boxItems.Insert(r((i + iCount - 1) % iCount), item); if (i == 0) break; } } box.UnselectAll(); TargetBoxItemsRefresh(box, boxItemsSource); box.SelectedItemsAdd(selected); box.ScrollIntoView(direction < 0 ? selected[0] : selected.Last()); } catch (Exception ex) { MessageBox.Show(ex.Message + "\r\n" + ex.StackTrace); } return true; }
/// <summary>選択アイテム追加・挿入</summary> public bool bxAddItems(IEnumerable srcList, ListBox target, bool IsInsert = false, IList trgItemsSource = null) { try { if (srcList == null || srcList.Cast<object>().Count() == 0 || target == null) return false; var trgItems = trgItemsSource ?? target.Items; var addList = srcList.Cast<object>() .Where(item => IsEnableDuplicate(item) == true || bxContains(target.Items, item) == false) .Select(item => IsEnableDuplicate(item) == true ? CloneObj(item) : item).ToList(); int scrollTo = target.SelectedIndex; if (IsInsert == true && target.SelectedIndex >= 0) { trgItems.InsertItemsAx(target.SelectedIndex, addList); } else { scrollTo = trgItems.AddItemsAx(addList); } target.UnselectAll(); TargetBoxItemsRefresh(target, trgItemsSource); target.SelectedItemsAdd(addList); if (target.SelectedIndex >= 0) target.ScrollIntoViewIndex(scrollTo); } catch (Exception ex) { MessageBox.Show(ex.Message + "\r\n" + ex.StackTrace); } return true; }