wpf TelerikUI使用DragDropManager

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

cs 复制代码
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

XML 复制代码
<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:

cs 复制代码
ApplicationList.ItemsSource = ApplicationInfo.GenerateApplicationInfos(); 
MyAppList.ItemsSource = new ObservableCollection<ApplicationInfo>(); 

附加拖动处理事件

cs 复制代码
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到所有允许拖动的场景

cs 复制代码
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 }; 
} 

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

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

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

cs 复制代码
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

相关推荐
微露清风10 分钟前
系统性学习C++-第十讲-stack 和 quene
java·c++·学习
闲人编程11 分钟前
Python游戏开发入门:Pygame实战
开发语言·python·游戏·pygame·毕设·codecapsule
一蓑烟雨任平生√14 分钟前
两种上传图片的方式——91张先生
java·ossinsight
是苏浙18 分钟前
零基础入门C语言之枚举和联合体
c语言·开发语言
报错小能手20 分钟前
C++笔记(面向对象)静态联编和动态联编
开发语言·c++·算法
凤凰战士芭比Q28 分钟前
部署我的世界-java版服务器-frp内网穿透
java·服务器
小肖爱笑不爱笑29 分钟前
2025/11/5 IO流(字节流、字符流、字节缓冲流、字符缓冲流) 计算机存储规则(ASCII、GBK、Unicode)
java·开发语言·算法
CodeCraft Studio40 分钟前
PPT处理控件Aspose.Slides教程:使用Java将PowerPoint笔记导出为PDF
java·笔记·pdf·powerpoint·aspose·ppt转pdf·java将ppt导出pdf
手握风云-42 分钟前
Java 数据结构第二十八期:反射、枚举以及 lambda 表达式
java·开发语言
ᐇ9591 小时前
Java Vector集合全面解析:线程安全的动态数组
java·开发语言