当前位置:  开发笔记 > 编程语言 > 正文

突出显示拖动的TreeView项目

如何解决《突出显示拖动的TreeView项目》经验,为你挑选了1个好方法。



1> Stefan..:

我正在使用附加属性,然后在我的xaml文件中使用该属性来更改树视图项的背景颜色:

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
{
    /// 
    /// Implements an attached property used for styling TreeViewItems when
    /// they're a possible drop target.
    /// 
    public static class TreeViewDropHighlighter
    {
        #region private variables
        /// 
        /// the TreeViewItem that is the current drop target
        /// 
        private static TreeViewItem _currentItem = null;

        /// 
        /// Indicates whether the current TreeViewItem is a possible
        /// drop target
        /// 
        private static bool _dropPossible;
        #endregion

        #region IsPossibleDropTarget
        /// 
        /// Property key (since this is a read-only DP) for the IsPossibleDropTarget property.
        /// 
        private static readonly DependencyPropertyKey IsPossibleDropTargetKey = 
                                    DependencyProperty.RegisterAttachedReadOnly(
                                        "IsPossibleDropTarget",
                                        typeof( bool ),
                                        typeof( TreeViewDropHighlighter ),
                                        new FrameworkPropertyMetadata( null,
                                            new CoerceValueCallback( CalculateIsPossibleDropTarget ) ) );


        /// 
        /// 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).
        /// 
        public static readonly DependencyProperty IsPossibleDropTargetProperty = IsPossibleDropTargetKey.DependencyProperty;

        /// 
        /// Getter for IsPossibleDropTarget
        /// 
        public static bool GetIsPossibleDropTarget( DependencyObject obj )
        {
            return (bool)obj.GetValue( IsPossibleDropTargetProperty );
        }

        /// 
        /// Coercion method which calculates the IsPossibleDropTarget property.
        /// 
        private static object CalculateIsPossibleDropTarget( DependencyObject item, object value )
        {
            if ( ( item == _currentItem ) && ( _dropPossible ) )
                return true;
            else
                return false;
        }
        #endregion

        /// 
        /// Initializes the  class.
        /// 
        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
        /// 
        /// Called when an item is dragged over the TreeViewItem.
        /// 
        /// The sender.
        /// The  instance containing the event data.
        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 );
                }
            }
        }

        /// 
        /// Called when the drag cursor leaves the TreeViewItem
        /// 
        /// The sender.
        /// The  instance containing the event data.
        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
    }
}

然后在xaml文件中:

    
        
    


这正是我今天所需要的.非常感谢Stefan!在我的树视图中,我注意到,有时它不会自动从我的drop中删除高亮,所以我注册了drop事件并编写了一个简单的函数,将_dropPossible设置为false并使Invalidates IsPossibleDropTargetProperty无效.对其他人有用吗?
推荐阅读
虎仔球妈_459
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有