Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>This post is simply a porting of Stefan's awesome response above to VB, for those of us working in those confines; I have nothing to offer except to hope I didn't screw anything up too badly. It appears to work for me:</p> <pre><code>Namespace SKNotes.Utilities ''' &lt;summary&gt; ''' Implements an attached property used for styling TreeViewItems when ''' they are a possible drop target. ''' &lt;/summary&gt; Public Class TreeViewDropHighlighter ''' &lt;summary&gt; ''' The TreeViewItem that is the current drop target ''' &lt;/summary&gt; Private Shared _CurrentItem As TreeViewItem = Nothing ''' &lt;summary&gt; ''' Indicates whether the current TreeView Item is a possible drop target ''' &lt;/summary&gt; ''' &lt;remarks&gt;&lt;/remarks&gt; Private Shared _DropPossible As Boolean ''' &lt;summary&gt; ''' Property Key (since this is a read only DP) for the IsPossibleDropTarget property. ''' &lt;/summary&gt; ''' &lt;remarks&gt;&lt;/remarks&gt; Private Shared ReadOnly IsPossibleDropTargetKey As DependencyPropertyKey = _ DependencyProperty.RegisterAttachedReadOnly _ ( _ "IsPossibleDropTarget", _ GetType(Boolean), _ GetType(TreeViewDropHighlighter), _ New FrameworkPropertyMetadata(Nothing, _ New CoerceValueCallback(AddressOf CalculateIsPossibleDropTarget) ) ) ''' &lt;summary&gt; ''' Dependency Property IsPossibleDropTarget. ''' Is true if the TreeViewItem is a possible drop target (ie, if it would receive the ''' OnDrop even if the mouse button is release right now). ''' &lt;/summary&gt; ''' &lt;remarks&gt;&lt;/remarks&gt; Public Shared ReadOnly IsPossibleDropTargetProperty As DependencyProperty = IsPossibleDropTargetKey.DependencyProperty ''' &lt;summary&gt; ''' Getter for IsPossibleDropTarget ''' &lt;/summary&gt; Public Shared Function GetIsPossibleDropTarget(ByVal obj As DependencyObject) As Boolean Return CBool(obj.GetValue(IsPossibleDropTargetProperty)) End Function ''' &lt;summary&gt; ''' Coercion method which calculates the IsPossibleDropTarget property ''' &lt;/summary&gt; Private Shared Function CalculateIsPossibleDropTarget(item As DependencyObject, value As Object) As Object If item.Equals(_CurrentItem) And _ _DropPossible Then Return True Else Return False End If End Function ''' &lt;summary&gt; ''' Initializes the TreeViewDropHighlighter class ''' &lt;/summary&gt; Shared Sub New() EventManager.RegisterClassHandler(GetType(TreeViewItem), _ TreeViewItem.PreviewDragEnterEvent, _ New DragEventHandler(AddressOf OnDragEvent), True) EventManager.RegisterClassHandler(GetType(TreeViewItem), _ TreeViewItem.PreviewDragLeaveEvent, New DragEventHandler(AddressOf OnDragLeave), True) EventManager.RegisterClassHandler(GetType(TreeViewItem), _ TreeViewItem.PreviewDragOverEvent, _ New DragEventHandler(AddressOf OnDragEvent), True) End Sub ''' &lt;summary&gt; ''' Called when an item is dragged over the TreeView Item ''' &lt;/summary&gt; ''' &lt;param name="sender"&gt;The sender&lt;/param&gt; ''' &lt;param name="args"&gt;The System.Windows.DragEventArgs instance containing the event data&lt;/param&gt; ''' &lt;remarks&gt;&lt;/remarks&gt; Shared Sub OnDragEvent(sender As Object, args As DragEventArgs) SyncLock (IsPossibleDropTargetProperty) _DropPossible = False If Not IsNothing(_CurrentItem) Then 'Tell the item that previously had the mouse that it no longer does. Dim OldItem As DependencyObject = _CurrentItem _CurrentItem = Nothing OldItem.InvalidateProperty(IsPossibleDropTargetProperty) End If If args.Effects &lt;&gt; DragDropEffects.None Then _DropPossible = True End If Dim tvi As TreeViewItem = CType(sender, TreeViewItem) If Not IsNothing(tvi) Then _CurrentItem = tvi 'Tell that item to recalculate the IsPossibleDropTarget property _CurrentItem.InvalidateProperty(IsPossibleDropTargetProperty) End If End SyncLock End Sub Shared Sub OnDragLeave(sender As Object, args As DragEventArgs) SyncLock (IsPossibleDropTargetProperty) _DropPossible = False If Not IsNothing(_CurrentItem) Then 'Tell the item that previously had the mouse that it no longer does Dim OldItem As DependencyObject = _CurrentItem _CurrentItem = Nothing OldItem.InvalidateProperty(IsPossibleDropTargetProperty) End If Dim tvi As TreeViewItem = CType(sender, TreeViewItem) If Not IsNothing(tvi) Then _CurrentItem = tvi tvi.InvalidateProperty(IsPossibleDropTargetProperty) End If End SyncLock End Sub End Class </code></pre> <p>End Namespace</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload