Wpf之TreeView实现简单的鼠标拖拽功能

2016-11-18 11:26:26来源:CSDN作者:Mrhan210人点击

第七城市

Wpf小白一个,现在正在积累经验,所以会把自己觉得有用上传到博客上,以方便以后碰到相似的问题容易查找。

这个Demo实现了简单的鼠标拖拽,其实原理很简单,在xaml的Treeview里设置Allowdrop=“true”,MouseMove=“treeView_MouseMove”

在treeView_MouseMove方法中写入  DragDrop.DoDragDrop(treeView1, item.Header.ToString(), DragDropEffects.Move);   ,基本上就可以实现拖拽功能

主要代码如下:

xaml文件代码:

<Window x:Class="Wpf1111.TreeView"        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"        Title="TreeView" Height="350" Width="600"   >    <Grid>        <Grid.ColumnDefinitions>            <ColumnDefinition Width="200" />            <ColumnDefinition Width="200" />            <ColumnDefinition Width="200" />             </Grid.ColumnDefinitions>        <TreeView  Grid.Column="0" Background="BurlyWood"   x:Name="treeView1"                   AllowDrop="True"  MouseMove="treeView_MouseMove" >            <TreeViewItem  Header="天津市"  MouseDoubleClick="tv_DoubleClick"      >                <TreeViewItem  Header="津南区"/>                <TreeViewItem  Header="南开区"/>                <TreeViewItem  Header="西青区"/>                <TreeViewItem  Header="滨海新区"/>            </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="reeView_DragOver"/>-->                    <!--<EventSetter Event="TreeViewItem.Drop" Handler="treeView_Drop"/>-->                    <!--<EventSetter Event="TreeViewItem.MouseMove" Handler="treeView_MouseMove"/>-->                              </Style>            </TreeView.ItemContainerStyle>                           </TreeView>        <Canvas  Name="Canvas1" Grid.Column="1" Background="YellowGreen" AllowDrop="True"     Drop="target1_Drop" DragEnter="target1_DragEnter"  DragLeave="target1_DragLeave" />        <Canvas  Name="Canvas2" Grid.Column="2" Background="Beige" AllowDrop="True" Margin="0,0,10,0"    Drop="target2_Drop"  DragEnter="target2_DragEnter" DragLeave="target2_DragLeave"></Canvas>    </Grid></Window>

后台主要代码:


    private void treeView_MouseMove(object sender,MouseEventArgs e)        {            TreeViewItem item = treeView1.SelectedItem as TreeViewItem;            try            {                if (e.LeftButton == MouseButtonState.Pressed)                {                                        //draggedItem = (TreeViewItem)treeView1.SelectedItem;                        //if (draggedItem != null)                        //{//传递数据                            DragDrop.DoDragDrop(treeView1, item.Header.ToString(),                               DragDropEffects.Move);                         //   MessageBox.Show("11111111   ");                //        }                                 }            }            catch (Exception)            {            }            }


第七城市

最新文章

123

最新摄影

微信扫一扫

第七城市微信公众平台