private void DragSource_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e) { _sourceItemsControl = (ItemsControl) sender; var visual = e.OriginalSource as Visual; _topWindow = Window.GetWindow(_sourceItemsControl); _initialMousePosition = e.GetPosition(_topWindow); _sourceItemContainer = _sourceItemsControl.ContainerFromElement(visual) as FrameworkElement; if (_sourceItemContainer != null) { _draggedData = _sourceItemContainer.DataContext; } }
public static DependencyObject ContainerFromElement (ItemsControl itemsControl, DependencyObject element) { return itemsControl.ContainerFromElement (element); }
// DragSource private void DragSource_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e) { this.sourceItemsControl = (ItemsControl)sender; Visual visual = e.OriginalSource as Visual; this.topWindow = Window.GetWindow(this.sourceItemsControl); this.initialMousePosition = e.GetPosition(this.topWindow); this.sourceItemContainer = sourceItemsControl.ContainerFromElement(visual) as FrameworkElement; if (this.sourceItemContainer != null) { //this.draggedData = this.sourceItemContainer.DataContext; this.draggedData = this.sourceItemContainer; // TODO changed } }
// DragSource Touch private void DragSource_PreviewTouchDown(object sender, TouchEventArgs e) { if (!_ignoreNextTouchDownEvent) { this.sourceItemsControl = (ItemsControl)sender; Visual visual = e.OriginalSource as Visual; this.topWindow = Window.GetWindow(this.sourceItemsControl); this.initialMousePosition = e.GetTouchPoint(this.topWindow).Position; this.sourceItemContainer = sourceItemsControl.ContainerFromElement(visual) as FrameworkElement; if (this.sourceItemContainer != null) { this.draggedData = this.sourceItemContainer.DataContext; //Console.WriteLine("preview mouse down, initiate drag"); } } else { _ignoreNextTouchDownEvent = false; } }
// DragSource private void DragSource_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e) { this.sourceItemsControl = (ItemsControl)sender; DependencyObject visual = e.OriginalSource as DependencyObject; if (visual == null) return; this.sourceItemContainer = sourceItemsControl.ContainerFromElement(visual) as FrameworkElement; if (this.sourceItemContainer == null) return; bool? canDrag = sourceItemContainer.GetValue(DragDrop.DragDropHelper.IsDragAllowedProperty) as bool?; if (canDrag.HasValue && !canDrag.Value) return; this.draggedData = this.sourceItemContainer.DataContext; this.topWindow = Window.GetWindow(this.sourceItemsControl); this.initialMousePosition = e.GetPosition(this.topWindow); }
private void dragSource_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e) { sourceItemsControl = (ItemsControl)sender; Visual visual = e.OriginalSource as Visual; topWindow = Window.GetWindow(sourceItemsControl); sourceItemContainer = sourceItemsControl.ContainerFromElement(visual) as FrameworkElement; if(sourceItemContainer!=null) { draggedData = sourceItemContainer.DataContext; } }
// DragSource private void DragSource_PreviewMouseDoubleClick(object sender, MouseButtonEventArgs e) { this._sourceItemsControl = (ItemsControl)sender; var visual = e.OriginalSource as Visual; this._sourceItemContainer = _sourceItemsControl.ContainerFromElement(visual) as FrameworkElement; if (this._sourceItemContainer != null) { this._draggedData = this._sourceItemContainer.DataContext; } int indexRemoved = RemoveItemFromItemsControl(this._sourceItemsControl, this._draggedData); Type draggedType = this._draggedData.GetType(); object newitem = Activator.CreateInstance(draggedType); InsertItemInItemsControl(this._sourceItemsControl, newitem, indexRemoved); e.Handled = true; }
private DependencyObject GetGeneratedParentElement(ItemsControl itemsControl) { return itemsControl.ContainerFromElement(this); }
private int DecideDropTarget(DragEventArgs e, ItemsControl targetItemsControl) { int targetItemsControlCount = targetItemsControl.Items.Count; DependencyObject targetItemContainer; int insertionIndex = -1; if (targetItemsControlCount > 0) { targetItemContainer = targetItemsControl.ContainerFromElement((DependencyObject)e.OriginalSource) as FrameworkElement; if (targetItemContainer != null) { insertionIndex = targetItemsControl.ItemContainerGenerator.IndexFromContainer(targetItemContainer); } else { targetItemContainer = targetItemsControl.ItemContainerGenerator.ContainerFromIndex(targetItemsControlCount - 1) as FrameworkElement; insertionIndex = targetItemsControlCount; } } else { targetItemContainer = null; insertionIndex = 0; } return insertionIndex; }