Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 145503
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T08:26:07+00:00 2026-05-11T08:26:07+00:00

In my application, I have a TreeView that allows drag/drop. I have all the

  • 0

In my application, I have a TreeView that allows drag/drop. I have all the functionality working fine, however I am having difficulty highlighting a TreeViewItem when it is dragged over. Here is my style for my treeview item. The IsMouseOver trigger does not work while dragging, because dragging seems to block other mouse events. Can anyone help me trigger the same border changes on my treeview item while dragging?

<Style x:Key='TreeViewItemStyle' TargetType='{x:Type TreeViewItem}'>      <Setter Property='Template'>          <Setter.Value>              <ControlTemplate TargetType='{x:Type TreeViewItem}'>                  <Grid>                      <Grid.ColumnDefinitions>                          <ColumnDefinition MinWidth='19' Width='Auto'/>                          <ColumnDefinition Width='Auto'/>                          <ColumnDefinition Width='*'/>                      </Grid.ColumnDefinitions>                      <Grid.RowDefinitions>                          <RowDefinition Height='Auto'/>                          <RowDefinition/>                      </Grid.RowDefinitions>                      <ToggleButton                           x:Name='PART_Expander'                          Style='{StaticResource ExpandCollapseToggleStyle}'                          IsChecked='{Binding Path=IsExpanded, RelativeSource={RelativeSource TemplatedParent}}'                          ClickMode='Press'                          />                      <Border                          x:Name='OuterBorder'                           Grid.Column='1'                          SnapsToDevicePixels='True'                          BorderThickness='1'                           CornerRadius='3'                           BorderBrush='Transparent'                           Background='Transparent'                          >                          <Border                               x:Name='InnerBorder'                               SnapsToDevicePixels='True'                              BorderThickness='1'                               CornerRadius='2'                               BorderBrush='Transparent'                               Background='Transparent'                              >                              <ContentPresenter                                  x:Name='PART_Content'                                  ContentSource='Header'                                  HorizontalAlignment='{TemplateBinding HorizontalContentAlignment}'                                  />                          </Border>                      </Border>                      <ItemsPresenter                          x:Name='PART_ItemsHost'                          Grid.Row='1'                          Grid.Column='1'                          Grid.ColumnSpan='2'                          />                  </Grid>                  <ControlTemplate.Triggers>                      <Trigger Property='IsMouseOver' SourceName='OuterBorder' Value='True'>                          <Setter TargetName='OuterBorder' Property='BorderBrush' Value='Blue' />                          <Setter TargetName='OuterBorder' Property='Background' Value='Red' />                          <Setter TargetName='InnerBorder' Property='BorderBrush' Value='White' />                      </Trigger>                      <MultiTrigger>                  </ControlTemplate.Triggers>              </ControlTemplate>          </Setter.Value>      </Setter>  </Style> 
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T08:26:07+00:00Added an answer on May 11, 2026 at 8:26 am

    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:

    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 {     /// <summary>     /// Implements an attached property used for styling TreeViewItems when     /// they're a possible drop target.     /// </summary>     public static class TreeViewDropHighlighter     {         #region private variables         /// <summary>         /// the TreeViewItem that is the current drop target         /// </summary>         private static TreeViewItem _currentItem = null;          /// <summary>         /// Indicates whether the current TreeViewItem is a possible         /// drop target         /// </summary>         private static bool _dropPossible;         #endregion          #region IsPossibleDropTarget         /// <summary>         /// Property key (since this is a read-only DP) for the IsPossibleDropTarget property.         /// </summary>         private static readonly DependencyPropertyKey IsPossibleDropTargetKey =                                      DependencyProperty.RegisterAttachedReadOnly(                                         'IsPossibleDropTarget',                                         typeof( bool ),                                         typeof( TreeViewDropHighlighter ),                                         new FrameworkPropertyMetadata( null,                                             new CoerceValueCallback( CalculateIsPossibleDropTarget ) ) );           /// <summary>         /// 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).         /// </summary>         public static readonly DependencyProperty IsPossibleDropTargetProperty = IsPossibleDropTargetKey.DependencyProperty;          /// <summary>         /// Getter for IsPossibleDropTarget         /// </summary>         public static bool GetIsPossibleDropTarget( DependencyObject obj )         {             return (bool)obj.GetValue( IsPossibleDropTargetProperty );         }          /// <summary>         /// Coercion method which calculates the IsPossibleDropTarget property.         /// </summary>         private static object CalculateIsPossibleDropTarget( DependencyObject item, object value )         {             if ( ( item == _currentItem ) && ( _dropPossible ) )                 return true;             else                 return false;         }         #endregion          /// <summary>         /// Initializes the <see cref='TreeViewDropHighlighter'/> class.         /// </summary>         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         /// <summary>         /// Called when an item is dragged over the TreeViewItem.         /// </summary>         /// <param name='sender'>The sender.</param>         /// <param name='args'>The <see cref='System.Windows.DragEventArgs'/> instance containing the event data.</param>         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 );                 }             }         }          /// <summary>         /// Called when the drag cursor leaves the TreeViewItem         /// </summary>         /// <param name='sender'>The sender.</param>         /// <param name='args'>The <see cref='System.Windows.DragEventArgs'/> instance containing the event data.</param>         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     } } 

    and then in the xaml file:

        <TreeView.ItemContainerStyle>         <Style TargetType='{x:Type TreeViewItem}'>             <Setter Property='FontWeight' Value='Normal' />             <Style.Triggers>                 <Trigger Property='utils:TreeViewDropHighlighter.IsPossibleDropTarget' Value='True'>                     <Setter Property='Background' Value='{DynamicResource {x:Static SystemColors.HighlightBrushKey}}' />                 </Trigger>             </Style.Triggers>         </Style>     </TreeView.ItemContainerStyle> 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In my winform application I have a treeview. To give the idea that a
I have a TreeView control in a Windows Forms application that is displaying my
I have an Application layout that is based on a treeView at the left,
I have an ASP.NET MVC application that has a jQuery Treeview and a jQuery
In my 3d application, I have a TreeView that is databound to an ObservableCollection
I have a TreeView control in my WinForms .NET application that has multiple levels
I have an application that reads database tables and puts it into a treeview.
I have a wpf c# application, that loads tasks to a treeView from a
I am working on a WPF application which has a treeview that represents an
I have a WPF application that includes a TreeView . The user adds content

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.