private void ContextMenu_MenuItem_MoveSourceUpOrDown(object sender, RoutedEventArgs e) { MenuItem mi = sender as MenuItem; if (mi == null) { return; } SourceItem selectedItem = this.ListView_Sources.SelectedItem as SourceItem; this.MoveSourceItemUpOrDown(selectedItem, (string)mi.Tag == "up"); }
private void LoadSources() { List <SourceItem> sourceItems = new List <SourceItem>(); foreach (Source source in App.sources.SourcesList) { SourceItem sourceItem = new SourceItem(source); sourceItem.MoveUpRequested += new EventHandler(this.SourceItem_MoveUp); sourceItem.MoveDownRequested += new EventHandler(this.SourceItem_MoveDown); sourceItems.Add(sourceItem); } this.UpdateSources(sourceItems); }
/// <summary> /// Move a SourceItem up or down in the Sources list. /// </summary> /// <param name="selectedItem"></param> /// <param name="up"></param> private void MoveSourceItemUpOrDown(SourceItem selectedItem, bool up) { int indexOfSelectedItem = this.ListView_Sources.Items.IndexOf(selectedItem); // Don't move source up if it's already at the top if (up && indexOfSelectedItem < 1) { return; } else if (!up && indexOfSelectedItem == this.ListView_Sources.Items.Count - 1) { return; } SourceItem otherItem = this.ListView_Sources.Items.GetItemAt(indexOfSelectedItem + (up ? -1 : 1)) as SourceItem; // Exchange ordering with the item above byte selectedItemOrdering = selectedItem.Ordering; byte otherItemOrdering = otherItem.Ordering; if (selectedItemOrdering == otherItemOrdering) { if (up) { otherItemOrdering -= 1; } else { otherItemOrdering += 1; } } selectedItem.Ordering = otherItemOrdering; otherItem.Ordering = selectedItemOrdering; // Build new list List <SourceItem> sourceItems = new List <SourceItem>(); foreach (SourceItem sourceItem in this.ListView_Sources.Items) { sourceItems.Add(sourceItem); } // Update sources this.ListView_Sources.Items.Clear(); this.UpdateSources(sourceItems); }
private void ListView_Sources_Drop(object sender, DragEventArgs e) { SourceItem droppedData = e.Data.GetData(typeof(SourceItem)) as SourceItem; SourceItem target = ((ListViewItem)(sender)).DataContext as SourceItem; sbyte removedIndex = (sbyte)ListView_Sources.Items.IndexOf(droppedData); sbyte targetIndex = (sbyte)ListView_Sources.Items.IndexOf(target); if (removedIndex == targetIndex) { return; } this.ListView_Sources.Items.RemoveAt(removedIndex); this.ListView_Sources.Items.Insert(targetIndex, droppedData); // Update tag indexes for ordering foreach (SourceItem sourceItem in this.ListView_Sources.Items) { sourceItem.Ordering = (byte)(1 + this.ListView_Sources.Items.IndexOf(sourceItem)); } }
/// <summary> /// Called when clicking on the "Down" button on a SourceItem. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void SourceItem_MoveDown(object sender, EventArgs e) { SourceItem sourceItem = sender as SourceItem; this.MoveSourceItemUpOrDown(sourceItem, false); }