2009-03-12 15 views
5

En mi aplicación, tengo un TreeView que permite arrastrar/soltar. Tengo toda la funcionalidad funcionando bien, sin embargo, tengo dificultades para resaltar un TreeViewItem cuando se arrastra. Este es mi estilo para mi artículo treeview. El desencadenador IsMouseOver no funciona mientras se arrastra, porque el arrastre parece bloquear otros eventos del mouse. ¿Alguien puede ayudarme a activar los mismos cambios de borde en mi elemento treeview mientras arrastro?Resalte el elemento TreeView arrastrado sobre

<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> 

Respuesta

16

estoy usando una propiedad adjunta para esto, y luego usar esa propiedad en mi archivo XAML para cambiar el color de fondo del elemento de vista de árbol:

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 
    } 
} 

y luego en el archivo XAML:

<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

Eso es algo hermoso, ¡muchas gracias! –

+2

Esto es exactamente lo que necesitaba hoy. Muchas gracias Stefan! En mi vista de árbol noté sin embargo que a veces no eliminaba automáticamente el resaltado de mi caída, así que registré el evento de caída y escribí una función simple que establece _dropPossible de nuevo en falso e Invalida IsPossibleDropTargetProperty. Tal vez sea útil para alguien más? – chocojosh

+0

¡Buen trabajo! Una cosa que agregué además de lo que #chocojosh agregó, es decirle al TreeViewItem que se expanda si tiene elementos en el evento OnDragEvent. Puede hacer esto agregando: if (_currentItem.HasItems) _currentItem.IsExpanded = true; – Brent

4

Mire el caso DragOver (y posiblemente DragEnter/DragLeave) en lugar de IsMouseOver.

+0

"DragOver" no es una propiedad válida para establecer un activador de la misma manera que "IsMouseOver". Puedo usar un EventTrigger, sin embargo, parece que solo aceptan StoryBoards y no puedo entender cómo configurar las propiedades de mi frontera dentro de StoryBoard de la misma manera que lo hice con los Setter de IsMouseOver. –

+0

Tuve que agregar una línea al constructor TreeViewDropHighlighter(), para eliminar también los cambios después de hacer un drop .... EventManager.RegisterClassHandler (typeof (TreeViewItem), TreeViewItem.PreviewDropEvent, nuevo DragEventHandler (OnDragLeave), true) ; –

+0

Es un evento. Lo siento, no estaba claro! Puede usar un organizador de eventos en su estilo para agregarlo a todos sus elementos de vista de árbol. –

1

Esta publicación es simplemente un reflejo de la impresionante respuesta de Stefan anterior a VB, para aquellos de nosotros que trabajamos en esos confines; No tengo nada que ofrecer excepto esperar a no haber metido la pata demasiado mal. Parece que funciona para mí:

Namespace SKNotes.Utilities 

''' <summary> 
''' Implements an attached property used for styling TreeViewItems when 
''' they are a possible drop target. 
''' </summary> 
Public Class TreeViewDropHighlighter 

    ''' <summary> 
    ''' The TreeViewItem that is the current drop target 
    ''' </summary> 
    Private Shared _CurrentItem As TreeViewItem = Nothing 

    ''' <summary> 
    ''' Indicates whether the current TreeView Item is a possible drop target 
    ''' </summary> 
    ''' <remarks></remarks> 
    Private Shared _DropPossible As Boolean 

    ''' <summary> 
    ''' Property Key (since this is a read only DP) for the IsPossibleDropTarget property. 
    ''' </summary> 
    ''' <remarks></remarks> 
    Private Shared ReadOnly IsPossibleDropTargetKey As DependencyPropertyKey = _ 
              DependencyProperty.RegisterAttachedReadOnly _ 
              (_ 
               "IsPossibleDropTarget", _ 
               GetType(Boolean), _ 
               GetType(TreeViewDropHighlighter), _ 
               New FrameworkPropertyMetadata(Nothing, _ 
                       New CoerceValueCallback(AddressOf CalculateIsPossibleDropTarget) 
                       ) 
              ) 
    ''' <summary> 
    ''' 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). 
    ''' </summary> 
    ''' <remarks></remarks> 
    Public Shared ReadOnly IsPossibleDropTargetProperty As DependencyProperty = IsPossibleDropTargetKey.DependencyProperty 

    ''' <summary> 
    ''' Getter for IsPossibleDropTarget 
    ''' </summary> 
    Public Shared Function GetIsPossibleDropTarget(ByVal obj As DependencyObject) As Boolean 
     Return CBool(obj.GetValue(IsPossibleDropTargetProperty)) 
    End Function 

    ''' <summary> 
    ''' Coercion method which calculates the IsPossibleDropTarget property 
    ''' </summary> 
    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 

    ''' <summary> 
    ''' Initializes the TreeViewDropHighlighter class 
    ''' </summary> 
    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 

    ''' <summary> 
    ''' Called when an item is dragged over the TreeView Item 
    ''' </summary> 
    ''' <param name="sender">The sender</param> 
    ''' <param name="args">The System.Windows.DragEventArgs instance containing the event data</param> 
    ''' <remarks></remarks> 
    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 <> 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 

Espacio de nombres Fin

+0

Gracias dansan, ¡funciona perfectamente! Nosotros los chicos de VB somos una especie en extinción y necesitamos unirnos. –

1

Además de la respuesta de Stefan, he encontrado que es necesario añadir también el evento Drop:

static void OnDragDrop(object sender, DragEventArgs args) 
{ 
    lock (IsPossibleDropTargetProperty) 
    { 
     _dropPossible = false; 

     if (_currentItem != null) 
     { 
      _currentItem.InvalidateProperty(IsPossibleDropTargetProperty); 
     } 

     TreeViewItem tvi = sender as TreeViewItem; 
     if (tvi != null) 
     { 
      tvi.InvalidateProperty(IsPossibleDropTargetProperty); 
     } 
    } 
} 

y el registro de evento de colocación también :

EventManager.RegisterClassHandler(typeof(TreeViewItem), 
      TreeViewItem.PreviewDropEvent, 
      new DragEventHandler(OnDragDrop), true); 

De lo contrario, si se produce una caída de un elemento de árbol sobre otro, el fondo podría no cambiar Lo mismo si presionas ESC.

Cuestiones relacionadas