• 拖放功能


    关于拖放,可以通过以下博客对拖放之间的事件关系进行了解
    http://www.cnblogs.com/loveis715/archive/2011/12/05/2277384.html


    拖放的公用类库 Platform.Common中的DragAndDrop,其中
    DragDropAdorner用来显示被拖拽的界面元素的预览
    Win32:用于返回鼠标位置
    Utils:获取拖放的项

    以上直接引用即可,具体拖放的实现要根据【拖拽源】和【拖拽目标】的不同以及值的不同处理而自行编写

    以下为将TreeView子项拖放到ListBox中的示例

    XML中

    【拖放源】
    1.设置属性AllowDrop="True"
    <TreeView x:Name="DragTreeSource" ItemsSource="{Binding OcComDepRoleTree}"
    BorderThickness="0" Background="#D3E2C5" Margin="10,5,0,5"
    ItemTemplate="{StaticResource ItemNode}"
    HorizontalAlignment="Left" Width="200"
    AllowDrop="True">
    </TreeView>
    2.添加事件TreeViewItem事件
    <Style TargetType="TreeViewItem">
    <EventSetter Event="TreeViewItem.PreviewMouseMove" Handler="OnPreviewListBoxMouseMove"/>
    <EventSetter Event="TreeViewItem.QueryContinueDrag" Handler="OnQueryContinueDrag"/>
    </Style>
    note:此时的TreeView的Style不能设置为静态的资源了,样式什么的都要自己写进来,以上省去为了突出事件

    【拖放目标】
    1.设置属性AllowDrop="True";
    2.绑定事件命令 DragDrop.DragOver="OnSignDragOver" DragDrop.Drop="OnSignDrop"
    <ListBox x:Name="lstSign" Background="#D3E2C5"
    AllowDrop="True"
    DragDrop.DragOver="OnSignDragOver" DragDrop.Drop="OnSignDrop"
    ItemsSource="{Binding OcDepForSign, Mode=TwoWay}" />


    CS 中
    AdornerLayer mAdornerLayer = null; //装饰器
    【拖放源】
    private void OnQueryContinueDrag(object sender, QueryContinueDragEventArgs e)
    {
    mAdornerLayer.Update();
    }
    private void OnPreviewListBoxMouseMove(object sender, MouseEventArgs e)
    {
    if (Mouse.LeftButton != MouseButtonState.Pressed)
    return;

    Point pos = e.GetPosition(DragTreeSource);
    HitTestResult result = VisualTreeHelper.HitTest(DragTreeSource, pos);
    if (result == null)
    return;

    TreeViewItem treeItem = Utils.FindVisualParent<TreeViewItem>(result.VisualHit); // Find your actual visual you want to drag
    if (treeItem == null || treeItem.DataContext != DragTreeSource.SelectedItem || !(DragTreeSource.SelectedItem is BaseTreeModel))
    return;


    DragDropAdorner adorner = new DragDropAdorner(treeItem); //(DragDropAdorner实现Adorner抽象类)

    mAdornerLayer = AdornerLayer.GetAdornerLayer(DragTreeSource);

    mAdornerLayer.Add(adorner);

    BaseTreeModel dataItem = treeItem.DataContext as BaseTreeModel;

    DataObject dob = new DataObject(dataItem);

    DragDrop.DoDragDrop(DragTreeSource, dob, DragDropEffects.Copy);//启动拖拽

    mAdornerLayer.Remove(adorner);
    }

    【拖放目标】

    private void OnSignDragOver(object sender, DragEventArgs e)
    {
    OnDragOver(e,lstSign);
    }
    private void OnSignDrop(object sender, DragEventArgs e)
    {
    OnDrag(e, lstSign);
    }

    private void OnDrag(DragEventArgs e ,ListBox lst)
    {
    try
    {
    Point pos = e.GetPosition(lst);
    HitTestResult result = VisualTreeHelper.HitTest(lst, pos);
    if (result == null)
    return;
    //获取拖放数据
    BaseTreeModel dataItdem = e.Data.GetData(typeof(BaseTreeModel)) as BaseTreeModel;
    //数据处理

    }
    catch (Exception ex)
    {
    throw ex;
    }
    }
    private void OnDragOver(DragEventArgs e,ListBox lst)
    {
    e.Effects = DragDropEffects.None;

    Point pos = e.GetPosition(lst);
    HitTestResult result = VisualTreeHelper.HitTest(lst, pos);
    if (result == null)
    return;

    ListBoxItem selectedItem = Utils.FindVisualParent<ListBoxItem>(result.VisualHit);
    if (selectedItem != null)
    selectedItem.IsSelected = true;
    e.Effects = DragDropEffects.Copy;
    }

  • 相关阅读:
    Dubbo-2.7.3升级-依赖问题
    Foreach删除元素(ArrayList)报错分析
    Dubbo日志打印级别调整
    kafka分区和副本如何分配
    IDEA 不能正确反编译 class /* compile ... */
    Mybatis获取插入数据的主键时,返回值总是1
    python 3.7.2 安装 pycurl 遇到的坑
    安装Web模块tornado,启动一直报ModuleNotFoundError: No module named 'tornado.ioloop'; 'tornado' is not a package
    【C#】HTTP POST 上传图片及参数
    【WPF】将DataGrid内容导出到Excel
  • 原文地址:https://www.cnblogs.com/mamaxiaoling/p/9879416.html
Copyright © 2020-2023  润新知