当前位置: 首页 > news >正文

wpf TelerikUI使用DragDropManager

首先,我先创建事务对象ApplicationInfo,当暴露出一对属性当例子集合对于构成ListBoxes。这个类在例子中显示如下代码:
 

public class ApplicationInfo 
{ public Double Price { get; set; } public String IconPath { get; set; } public String Name { get; set; } public String Author { get; set; } public static ObservableCollection<ApplicationInfo> GenerateApplicationInfos() { ObservableCollection<ApplicationInfo> result = new ObservableCollection<ApplicationInfo>(); ApplicationInfo info1 = new ApplicationInfo(); info1.Name = "Large Collider"; info1.Author = "C.E.R.N."; info1.IconPath = @"img/Atom.png"; result.Add(info1); ApplicationInfo info2 = new ApplicationInfo(); info2.Name = "Paintbrush"; info2.Author = "Imagine Inc."; info2.IconPath = @"img/Brush.png"; result.Add(info2); ApplicationInfo info3 = new ApplicationInfo(); info3.Name = "Lively Calendar"; info3.Author = "Control AG"; info3.IconPath = @"img/CalendarEvents.png"; result.Add(info3); ApplicationInfo info4 = new ApplicationInfo(); info4.Name = "Fire Burning ROM"; info4.Author = "The CD Factory"; info4.IconPath = @"img/CDBurn.png"; result.Add(info4); ApplicationInfo info5 = new ApplicationInfo(); info5.Name = "Fav Explorer"; info5.Author = "Star Factory"; info5.IconPath = @"img/favorites.png"; result.Add(info5); ApplicationInfo info6 = new ApplicationInfo(); info6.Name = "IE Fox"; info6.Author = "Open Org"; info6.IconPath = @"img/Connected.png"; result.Add(info6); ApplicationInfo info7 = new ApplicationInfo(); info7.Name = "Charting"; info7.Author = "AA-AZ inc"; info7.IconPath = @"img/ChartDot.png"; result.Add(info7); ApplicationInfo info8 = new ApplicationInfo(); info8.Name = "SuperPlay"; info8.Author = "EB Games"; info8.IconPath = @"img/Games.png"; result.Add(info8); return result; } 
} 

然后需要定义ListBoxes对于符合的ItemTemplates。我同样启动拖动ListBoxItems和允许拖动每一个ListBoxes

<Grid x:Name="LayoutRoot" Background="White"> <Grid.Resources> <Style TargetType="ListBoxItem"> <Setter Property="telerik:DragDropManager.AllowCapturedDrag" Value="True"></Setter> </Style> <DataTemplate x:Key="ApplicationTemplate"> <StackPanel Orientation="Horizontal"> <Image Source="{Binding IconPath}"/> <TextBlock Margin="5" Text="{Binding Name}" VerticalAlignment="Center"></TextBlock> </StackPanel> </DataTemplate> </Grid.Resources> <Grid.ColumnDefinitions> <ColumnDefinition></ColumnDefinition> <ColumnDefinition></ColumnDefinition> </Grid.ColumnDefinitions> <ListBox x:Name="ApplicationList" ItemTemplate="{StaticResource ApplicationTemplate}" AllowDrop="True"/> <ListBox x:Name="MyAppList" Background="Gray" Grid.Column="1"   ItemTemplate="{StaticResource ApplicationTemplate}" AllowDrop="True"/> 
</Grid> 

之后我们需要设置控件的ItemSource:

ApplicationList.ItemsSource = ApplicationInfo.GenerateApplicationInfos(); 
MyAppList.ItemsSource = new ObservableCollection<ApplicationInfo>(); 

附加拖动处理事件

DragDropManager.AddDragInitializeHandler(ApplicationList, OnDragInitialize); 
DragDropManager.AddDragInitializeHandler(MyAppList, OnDragInitialize); DragDropManager.AddGiveFeedbackHandler(ApplicationList, OnGiveFeedback); 
DragDropManager.AddGiveFeedbackHandler(MyAppList, OnGiveFeedback); DragDropManager.AddDragDropCompletedHandler(ApplicationList, OnDragCompleted); 
DragDropManager.AddDragDropCompletedHandler(MyAppList, OnDragCompleted); DragDropManager.AddDropHandler(ApplicationList, OnDrop); 
DragDropManager.AddDropHandler(MyAppList, OnDrop);   

然后在DraInitialize定义数据当拖动后,同时呈现。同样设置了DragDropEffects到所有允许拖动的场景

private void OnDragInitialize(object sender, DragInitializeEventArgs args) 
{ args.AllowedEffects = DragDropEffects.All; var payload = DragDropPayloadManager.GeneratePayload(null); var data = ((FrameworkElement)args.OriginalSource).DataContext; payload.SetData("DragData", data); args.Data = payload; args.DragVisual = new ContentControl { Content = data, ContentTemplate = LayoutRoot.Resources["ApplicationTemplate"] as DataTemplate }; 
} 

同样设置鼠标光标为箭头:
 

private void OnGiveFeedback(object sender, Telerik.Windows.DragDrop.GiveFeedbackEventArgs args) 
{ args.SetCursor(Cursors.Arrow); args.Handled = true; 
} 

最后添加逻辑,将在拖动后允许完成操作:
 

private void OnDrop(object sender, Telerik.Windows.DragDrop.DragEventArgs args) 
{ var data = ((DataObject)args.Data).GetData("DragData"); ((IList)(sender as ListBox).ItemsSource).Add(data); 
} public void OnDragCompleted(object sender, Telerik.Windows.DragDrop.DragDropCompletedEventArgs args) 
{ var data = DragDropPayloadManager.GetDataFromObject(args.Data, "DragData"); ((IList)(sender as ListBox).ItemsSource).Remove(data); 
} 

拖动如下ListBoxes

相关文章:

  • Kotlin ArrayList类型toTypedArray转换Array
  • RHEL8_Linux使用podman管理容器
  • 贝蒂详解<string.h>哦~(用法与实现)
  • KubeKey 离线部署 KubeSphere v3.4.1 和 K8s v1.26 实战指南
  • JVM 调优
  • 大华 DSS 数字监控系统 itcBulletin SQL 注入漏洞复现
  • 软件测试计划文档
  • 论文写作☞结论怎么写?
  • maui中实现加载更多 RefreshView跟ListView(1)
  • LVS-DR模式
  • ssm基于MVC的舞蹈网站的设计与实现论文
  • C#中的封装、继承和多态
  • 深入理解JVM设计的精髓与独特之处
  • 虚幻学习笔记12—C++类的实例化
  • 微服务网关Gateway
  • 【402天】跃迁之路——程序员高效学习方法论探索系列(实验阶段159-2018.03.14)...
  • 【附node操作实例】redis简明入门系列—字符串类型
  • const let
  • CSS3 聊天气泡框以及 inherit、currentColor 关键字
  • Docker入门(二) - Dockerfile
  • FineReport中如何实现自动滚屏效果
  • Linux编程学习笔记 | Linux多线程学习[2] - 线程的同步
  • mac修复ab及siege安装
  • markdown编辑器简评
  • mysql_config not found
  • spring boot下thymeleaf全局静态变量配置
  • spring-boot List转Page
  • Vue 重置组件到初始状态
  • 聚类分析——Kmeans
  • 一个完整Java Web项目背后的密码
  • 基于django的视频点播网站开发-step3-注册登录功能 ...
  • #pragam once 和 #ifndef 预编译头
  • #vue3 实现前端下载excel文件模板功能
  • (4)STL算法之比较
  • (js)循环条件满足时终止循环
  • (webRTC、RecordRTC):navigator.mediaDevices undefined
  • (附源码)基于ssm的模具配件账单管理系统 毕业设计 081848
  • (三)Honghu Cloud云架构一定时调度平台
  • (万字长文)Spring的核心知识尽揽其中
  • (未解决)jmeter报错之“请在微信客户端打开链接”
  • (原)记一次CentOS7 磁盘空间大小异常的解决过程
  • (转)关于多人操作数据的处理策略
  • .NET Core 中的路径问题
  • .Net Memory Profiler的使用举例
  • .Net Remoting(分离服务程序实现) - Part.3
  • .net 中viewstate的原理和使用
  • .Net7 环境安装配置
  • .net获取当前url各种属性(文件名、参数、域名 等)的方法
  • .Net面试题4
  • .NET性能优化(文摘)
  • .net与java建立WebService再互相调用
  • @Controller和@RestController的区别?
  • @DependsOn:解析 Spring 中的依赖关系之艺术
  • @select 怎么写存储过程_你知道select语句和update语句分别是怎么执行的吗?
  • @基于大模型的旅游路线推荐方案