【问题标题】:Dragging item from DataGridDragDropTarget to Label将项目从 DataGridDragDropTarget 拖动到标签
【发布时间】:2012-01-22 19:28:22
【问题描述】:

我正在使用 Silverlight Toolkit(2010 年 4 月)中的 ListBoxDragDropTarget 和 SL 4。

我想将列表框中的项目拖到Label 上并在那里处理放置事件。

但是看起来有点复杂。 Label 的常规 Drop 事件永远不会被触发。我想这是因为 Silverlight 工具包有自己的处理拖放的方式,这只是部分兼容。

环顾四周,我找到了Microsoft.Windows.DragDrop.DropEvent,并为该事件附加了一个处理程序。它奏效了!!我收到了Drop 事件。但是我不确定如何找到被拖动的真实对象(string)。

我试过e.Data.GetData(typeof(string)),但我什么也没得到。查看可用格式有一个System.Windows.Controls.ItemDragEventArgs 对象。在这里面我发现了一个System.Collections.ObjectModel.Selection 的数组,它有一个Item 属性。我想在这个 Item 属性中我找到了我的对象,但整个方法似乎有点脆弱,我不相信这是执行此操作的官方方法。

有没有更好的办法?

【问题讨论】:

    标签: c# silverlight silverlight-4.0 drag-and-drop silverlight-toolkit


    【解决方案1】:

    U 也可以使用另一个 ListBox 例如:包含命名空间

     xmlns:toolKit="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Toolkit"
    

    让我们在 Grid 中添加“ListBoxDragDropTarget”。将属性“AllowDrop”设置为 True。一旦设置为 true,它将能够在控件内捕获 drop 事件。 现在我们将在 ListBoxDragDropTarget 中添加一个 ListBox 并设置您喜欢的任何属性。假设

     <toolKit:ListBoxDragDropTarget AllowDrop="True">
       <ListBox x:Name="customerListBoxMain" Height="200" Width="200"
           DisplayMemberPath="Name">
        <ListBox.ItemsPanel>
          <ItemsPanelTemplate>
               <StackPanel Orientation="Vertical"/>
          </ItemsPanelTemplate>
        </ListBox.ItemsPanel>
       </ListBox>
      </toolKit:ListBoxDragDropTarget>
    

    并添加另一个列表框

      <toolKit:ListBoxDragDropTarget AllowDrop="True">
        <ListBox Height="200" Width="200" DisplayMemberPath="Name">
       <ListBox.ItemsPanel>
        <ItemsPanelTemplate>
          <StackPanel Orientation="Vertical"/>
        </ItemsPanelTemplate>
       </ListBox.ItemsPanel>
       </ListBox>
      </toolKit:ListBoxDragDropTarget>
    

    现在从后面的代码中获取一些数据并将其设置为第一个 ListBox 的 Source。下面是示例代码:

      public partial class MainPage : UserControl
      {
         public MainPage()
         {
            InitializeComponent();
    
            customerListBoxMain.ItemsSource = PersonDataProvider.GetData();
         }
       }
    

    完成了..

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-09-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多