none
wpf treeview 节点拖动时,目的节点显示一个下划线虚线 RRS feed

  • 问题

  • wpf treeview 节点拖动时,目的节点显示一个下划线虚线

    我在treeView拖动 这个地方下载了一个源码,但是我希望在他的基础上进行拖动的时候,目的节点显示一个虚线或者颜色啥的,表示我拖到了这个地方来了。

    就如上图的虚线一样。

    2018年5月2日 13:12

全部回复

  • 他具体的代码:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Data;
    using System.Windows.Documents;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Imaging;
    using System.Windows.Navigation;
    using System.Windows.Shapes;
    
    namespace WpfTreeView
    {
        /// <summary>
        /// Interaction logic for Window1.xaml
        /// </summary>
        public partial class Window1 : Window
        {
            Point _lastMouseDown;
            TreeViewItem draggedItem, _target;
            public Window1()
            {
                InitializeComponent();
            }
    
            private void TreeView_MouseDown(object sender, MouseButtonEventArgs e)
            {
                if (e.ChangedButton == MouseButton.Left)
                {
                    _lastMouseDown = e.GetPosition(tvParameters);
                }
    
            }
            private void treeView_MouseMove(object sender, MouseEventArgs e)
            {
                try
                {
                    if (e.LeftButton == MouseButtonState.Pressed)
                    {
                        Point currentPosition = e.GetPosition(tvParameters);
    
                        
                        if ((Math.Abs(currentPosition.X - _lastMouseDown.X) > 10.0) ||
                            (Math.Abs(currentPosition.Y - _lastMouseDown.Y) > 10.0))
                        {
                            draggedItem = (TreeViewItem)tvParameters.SelectedItem;
                            if (draggedItem != null)
                            {
                                DragDropEffects finalDropEffect = DragDrop.DoDragDrop(tvParameters, tvParameters.SelectedValue,
                                    DragDropEffects.Move);
                                //Checking target is not null and item is dragging(moving)
                                if ((finalDropEffect == DragDropEffects.Move) && (_target != null))
                                {
                                    // A Move drop was accepted
                                    if (!draggedItem.Header.ToString().Equals(_target.Header.ToString()))
                                    {
                                        CopyItem(draggedItem, _target);
                                        _target = null;
                                        draggedItem = null;
                                    }       
    
                                }
                            }
                        }
                    }
                }
                catch (Exception)
                {
                }
            }
            private void treeView_DragOver(object sender, DragEventArgs e)
            {
                try
                {
                       
                    Point currentPosition = e.GetPosition(tvParameters);
    
                  
                    if ((Math.Abs(currentPosition.X - _lastMouseDown.X) > 10.0) ||
                        (Math.Abs(currentPosition.Y - _lastMouseDown.Y) > 10.0))
                    {
                        // Verify that this is a valid drop and then store the drop target
                        TreeViewItem item = GetNearestContainer(e.OriginalSource as UIElement);
                        if (CheckDropTarget(draggedItem, item))
                        {
                            e.Effects = DragDropEffects.Move;
                        }
                        else
                        {
                            e.Effects = DragDropEffects.None;
                        }
                    }
                    e.Handled = true;
                }
                catch (Exception)
                {
                }
            }
            private void treeView_Drop(object sender, DragEventArgs e)
            {
                try
                {
                    e.Effects = DragDropEffects.None;
                    e.Handled = true;
    
                    // Verify that this is a valid drop and then store the drop target
                    TreeViewItem TargetItem = GetNearestContainer(e.OriginalSource as UIElement);
                    if (TargetItem != null && draggedItem != null )
                    {
                        _target = TargetItem;
                        e.Effects = DragDropEffects.Move;
    
                    }
                }
                catch (Exception)
                {
                }
    
    
    
            }
            private bool CheckDropTarget(TreeViewItem _sourceItem, TreeViewItem _targetItem)
            {
                //Check whether the target item is meeting your condition
                bool _isEqual = false;
                if (!_sourceItem.Header.ToString().Equals(_targetItem.Header.ToString()))
                {
                    _isEqual = true;
                }
                return _isEqual;
    
            }
            private void CopyItem(TreeViewItem _sourceItem, TreeViewItem _targetItem)
            {
    
                //Asking user wether he want to drop the dragged TreeViewItem here or not
                if (MessageBox.Show("Would you like to drop " + _sourceItem.Header.ToString() + " into " + _targetItem.Header.ToString() + "", "", MessageBoxButton.YesNo) == MessageBoxResult.Yes)
                {
                    try
                    {
                        //adding dragged TreeViewItem in target TreeViewItem
                        addChild(_sourceItem, _targetItem);
    
                        //finding Parent TreeViewItem of dragged TreeViewItem 
                        TreeViewItem ParentItem = FindVisualParent<TreeViewItem>(_sourceItem);
                        // if parent is null then remove from TreeView else remove from Parent TreeViewItem
                        if (ParentItem == null)
                        {
                            tvParameters.Items.Remove(_sourceItem);
                        }
                        else
                        {                        
                            ParentItem.Items.Remove(_sourceItem);
                        }
                    }
                    catch
                    {
    
                    }
                }
    
            }
            public void addChild(TreeViewItem _sourceItem,TreeViewItem _targetItem)
            {
                // add item in target TreeViewItem 
                TreeViewItem item1 = new TreeViewItem();
                item1.Header = _sourceItem.Header;
                _targetItem.Items.Add(item1);    
                foreach (TreeViewItem item in _sourceItem.Items)
                {
                    addChild(item, item1);               
                }
            }
            static TObject FindVisualParent<TObject>(UIElement child) where TObject : UIElement
            {
                if (child == null)
                {
                    return null;
                }
    
                UIElement parent = VisualTreeHelper.GetParent(child) as UIElement;
    
                while (parent != null)
                {
                    TObject found = parent as TObject;
                    if (found != null)
                    {
                        return found;
                    }
                    else
                    {
                        parent = VisualTreeHelper.GetParent(parent) as UIElement;
                    }
                }
    
                return null;
            }
            private TreeViewItem GetNearestContainer(UIElement element)
            {
                // Walk up the element tree to the nearest tree view item.
                TreeViewItem container = element as TreeViewItem;
                while ((container == null) && (element != null))
                {
                    element = VisualTreeHelper.GetParent(element) as UIElement;
                    container = element as TreeViewItem;
                }
                return container;
            }
    
        }
    }
    

    2018年5月2日 13:12
  • <Window x:Class="WpfTreeView.Window1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Drag and Drop Winodow" Height="376" Width="434">
        <Grid>
            <TreeView Name="tvParameters" AllowDrop="True" Background="PowderBlue" Foreground="DarkBlue" FontSize="15">
                <TreeViewItem Header="1" >
                    <TreeViewItem Header="1.1" >
                        <TreeViewItem Header="1.1.1" >
    
                        </TreeViewItem>
                    </TreeViewItem>
                </TreeViewItem>
                <TreeViewItem Header="2" >
                    <TreeViewItem Header="2.1" >
                        <TreeViewItem Header="2.1.1" >
                            <TreeViewItem Header="2.1.1.1" >
                                <TreeViewItem Header="2.1.1.1.1" >
                                    <TreeViewItem Header="测试1"/>
                                    <TreeViewItem Header="测试2"/>
                                    <TreeViewItem Header="测试3"/>
                                </TreeViewItem>
                            </TreeViewItem>
                        </TreeViewItem>
                    </TreeViewItem>
                <TreeViewItem Header="2.2" >
                        <TreeViewItem Header="2.2.1" AllowDrop="True">
                        </TreeViewItem>
                    </TreeViewItem>
                </TreeViewItem>
                <TreeView.ItemContainerStyle>
                    <Style TargetType="{x:Type TreeViewItem}">
                        <!-- We have to select the item which is right-clicked on -->
                        <!--<EventSetter Event="TreeViewItem.PreviewMouseRightButtonDown" Handler="TreeViewItem_PreviewMouseRightButtonDown"/>-->
                        <EventSetter Event="TreeViewItem.DragOver" Handler="treeView_DragOver"/>
                        <EventSetter Event="TreeViewItem.Drop" Handler="treeView_Drop"/>
                        <EventSetter Event="TreeViewItem.MouseMove" Handler="treeView_MouseMove"/>
    
    
    
    
                    </Style>
    
                </TreeView.ItemContainerStyle>
            </TreeView>
        </Grid>
    </Window>
    

    2018年5月2日 13:14
  • 你好,

    定义一个style 在xml文件中, 然后在Drag 和Drop 事件中更改target TreeviewItem 的Style. 类似于

    private void treeView_Drop(object sender, DragEventArgs e)
            {
                try
                {
                    e.Effects = DragDropEffects.None;
                    e.Handled = true;
                    // Verify that this is a valid drop and then store the drop target
                    TreeViewItem TargetItem = GetNearestContainer(e.OriginalSource as UIElement);
                    if (TargetItem != null && draggedItem != null )
                    {
                        TargetItem.Style = Resources["MouseOverTreeViewItem"] as Style;
                        _target = TargetItem;
                        e.Effects = DragDropEffects.Move;
                    }
                }
                catch (Exception)
                {
                }
            }

    #Style

     <Window.Resources>
            <Style x:Key="DefaultTreeViewItem" TargetType="{x:Type TreeViewItem}">
                <Setter Property="Background" Value="Transparent" />
            </Style>
            <Style x:Key="MouseOverTreeViewItem" TargetType="{x:Type TreeViewItem}">
                <Setter Property="Background" Value="AliceBlue" />
            </Style>
    
    
           
        </Window.Resources>

    Best regards,

    Zhanglong


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    2018年5月4日 6:32
  • 你好,

    定义一个style 在xml文件中, 然后在Drag 和Drop 事件中更改target TreeviewItem 的Style. 类似于

    private void treeView_Drop(object sender, DragEventArgs e)
            {
                try
                {
                    e.Effects = DragDropEffects.None;
                    e.Handled = true;
                    // Verify that this is a valid drop and then store the drop target
                    TreeViewItem TargetItem = GetNearestContainer(e.OriginalSource as UIElement);
                    if (TargetItem != null && draggedItem != null )
                    {
                        TargetItem.Style = Resources["MouseOverTreeViewItem"] as Style;
                        _target = TargetItem;
                        e.Effects = DragDropEffects.Move;
                    }
                }
                catch (Exception)
                {
                }
            }

    #Style

     <Window.Resources>
            <Style x:Key="DefaultTreeViewItem" TargetType="{x:Type TreeViewItem}">
                <Setter Property="Background" Value="Transparent" />
            </Style>
            <Style x:Key="MouseOverTreeViewItem" TargetType="{x:Type TreeViewItem}">
                <Setter Property="Background" Value="AliceBlue" />
            </Style>
    
    
           
        </Window.Resources>

    Best regards,

    Zhanglong


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    你好,我试过尝试按照你给的方式添加。但是死活不行。要么鼠标移上去的颜色一直存在,要么就没有颜色。

    因为按照你方式,只会在拖动放弃后,才会出现颜色,我想能够做到的是,拖上去后就出现颜色,拖动移开或者拖动完成所有颜色就还原了。

    2018年5月7日 8:33