Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I'm using an attached property for this, and then use that property in my xaml file to change the background color of the tree view item:</p> <pre><code>using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Windows; using System.Windows.Controls; using System.Windows.Input; namespace SKNotes.Utilities { /// &lt;summary&gt; /// Implements an attached property used for styling TreeViewItems when /// they're a possible drop target. /// &lt;/summary&gt; public static class TreeViewDropHighlighter { #region private variables /// &lt;summary&gt; /// the TreeViewItem that is the current drop target /// &lt;/summary&gt; private static TreeViewItem _currentItem = null; /// &lt;summary&gt; /// Indicates whether the current TreeViewItem is a possible /// drop target /// &lt;/summary&gt; private static bool _dropPossible; #endregion #region IsPossibleDropTarget /// &lt;summary&gt; /// Property key (since this is a read-only DP) for the IsPossibleDropTarget property. /// &lt;/summary&gt; private static readonly DependencyPropertyKey IsPossibleDropTargetKey = DependencyProperty.RegisterAttachedReadOnly( "IsPossibleDropTarget", typeof( bool ), typeof( TreeViewDropHighlighter ), new FrameworkPropertyMetadata( null, new CoerceValueCallback( CalculateIsPossibleDropTarget ) ) ); /// &lt;summary&gt; /// Dependency Property IsPossibleDropTarget. /// Is true if the TreeViewItem is a possible drop target (i.e., if it would receive /// the OnDrop event if the mouse button is released right now). /// &lt;/summary&gt; public static readonly DependencyProperty IsPossibleDropTargetProperty = IsPossibleDropTargetKey.DependencyProperty; /// &lt;summary&gt; /// Getter for IsPossibleDropTarget /// &lt;/summary&gt; public static bool GetIsPossibleDropTarget( DependencyObject obj ) { return (bool)obj.GetValue( IsPossibleDropTargetProperty ); } /// &lt;summary&gt; /// Coercion method which calculates the IsPossibleDropTarget property. /// &lt;/summary&gt; private static object CalculateIsPossibleDropTarget( DependencyObject item, object value ) { if ( ( item == _currentItem ) &amp;&amp; ( _dropPossible ) ) return true; else return false; } #endregion /// &lt;summary&gt; /// Initializes the &lt;see cref="TreeViewDropHighlighter"/&gt; class. /// &lt;/summary&gt; static TreeViewDropHighlighter( ) { // Get all drag enter/leave events for TreeViewItem. EventManager.RegisterClassHandler( typeof( TreeViewItem ), TreeViewItem.PreviewDragEnterEvent, new DragEventHandler( OnDragEvent ), true ); EventManager.RegisterClassHandler( typeof( TreeViewItem ), TreeViewItem.PreviewDragLeaveEvent, new DragEventHandler( OnDragLeave ), true ); EventManager.RegisterClassHandler( typeof( TreeViewItem ), TreeViewItem.PreviewDragOverEvent, new DragEventHandler( OnDragEvent ), true ); } #region event handlers /// &lt;summary&gt; /// Called when an item is dragged over the TreeViewItem. /// &lt;/summary&gt; /// &lt;param name="sender"&gt;The sender.&lt;/param&gt; /// &lt;param name="args"&gt;The &lt;see cref="System.Windows.DragEventArgs"/&gt; instance containing the event data.&lt;/param&gt; static void OnDragEvent( object sender, DragEventArgs args ) { lock ( IsPossibleDropTargetProperty ) { _dropPossible = false; if ( _currentItem != null ) { // Tell the item that previously had the mouse that it no longer does. DependencyObject oldItem = _currentItem; _currentItem = null; oldItem.InvalidateProperty( IsPossibleDropTargetProperty ); } if ( args.Effects != DragDropEffects.None ) { _dropPossible = true; } TreeViewItem tvi = sender as TreeViewItem; if ( tvi != null ) { _currentItem = tvi; // Tell that item to re-calculate the IsPossibleDropTarget property _currentItem.InvalidateProperty( IsPossibleDropTargetProperty ); } } } /// &lt;summary&gt; /// Called when the drag cursor leaves the TreeViewItem /// &lt;/summary&gt; /// &lt;param name="sender"&gt;The sender.&lt;/param&gt; /// &lt;param name="args"&gt;The &lt;see cref="System.Windows.DragEventArgs"/&gt; instance containing the event data.&lt;/param&gt; static void OnDragLeave( object sender, DragEventArgs args ) { lock ( IsPossibleDropTargetProperty ) { _dropPossible = false; if ( _currentItem != null ) { // Tell the item that previously had the mouse that it no longer does. DependencyObject oldItem = _currentItem; _currentItem = null; oldItem.InvalidateProperty( IsPossibleDropTargetProperty ); } TreeViewItem tvi = sender as TreeViewItem; if ( tvi != null ) { _currentItem = tvi; tvi.InvalidateProperty( IsPossibleDropTargetProperty ); } } } #endregion } } </code></pre> <p>and then in the xaml file:</p> <pre><code> &lt;TreeView.ItemContainerStyle&gt; &lt;Style TargetType="{x:Type TreeViewItem}"&gt; &lt;Setter Property="FontWeight" Value="Normal" /&gt; &lt;Style.Triggers&gt; &lt;Trigger Property="utils:TreeViewDropHighlighter.IsPossibleDropTarget" Value="True"&gt; &lt;Setter Property="Background" Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}" /&gt; &lt;/Trigger&gt; &lt;/Style.Triggers&gt; &lt;/Style&gt; &lt;/TreeView.ItemContainerStyle&gt; </code></pre>
    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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. 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