将项目从 DataGridDragDropTarget 拖动到 Label

发布于 2024-12-28 14:32:46 字数 649 浏览 5 评论 0原文

我正在将 Silverlight Toolkit(2010 年 4 月)中的 ListBoxDragDropTarget 与 SL 4 一起使用。

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

不过好像有点复杂。 Label 的常规 Drop 事件永远不会被触发。我想这是因为 Silverlight Toolkit 有自己的处理拖放的方式。删除仅部分兼容的内容。

环顾四周,我发现了 Microsoft.Windows.DragDrop.DropEvent 并将处理程序附加到该事件。它起作用了!我收到了 Drop 事件。但是我不确定如何获取被拖动的真实对象(字符串)。

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

还有更好的办法吗?

I'm using the ListBoxDragDropTarget from the Silverlight Toolkit (April 2010) with SL 4.

I want to drag items from the list box onto a Label and handle the drop event there.

However it seems a bit complicated. The regular Drop event of the Label never gets fired. I suppose that is because the Silverlight Toolkit has its own way of handling Drag & Drop which is only partially compatible.

Looking arround I found the Microsoft.Windows.DragDrop.DropEvent and attached a handler to this event. And it worked!! I got the Drop event. However I'm not sure how to get to the real object that was dragged (a string).

I tried e.Data.GetData(typeof(string)) but I got nothing. Looking at the available formats there is a System.Windows.Controls.ItemDragEventArgs object. Inside this I found an array of System.Collections.ObjectModel.Selection which then has an Item property. I suppose in this Item property I find my object, but the whole method seems a bit fragile and I'm not convinced that is the official way to do this.

Is there any better way?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

战皆罪 2025-01-04 14:32:46

你也可以使用另一个列表框
例如:include 命名空间

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

让我们在网格内添加“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>

现在添加另一个 ListBox

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

以获取一些数据并将其设置为后面代码中第一个 ListBox 的源。这是示例代码:

  public partial class MainPage : UserControl
  {
     public MainPage()
     {
        InitializeComponent();

        customerListBoxMain.ItemsSource = PersonDataProvider.GetData();
     }
   }

已完成..

U can also use another ListBox
For ex: include namespace

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

Let us add the “ListBoxDragDropTarget inside the Grid. Set the attribute “AllowDrop” to True. Once it is set to true, it will be able to catch the drop event inside the control.
Now we will add a ListBox inside the ListBoxDragDropTarget and set properties whichever u like. suppose

 <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>

And add another ListBox

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

Now to fetch some data and set it to the Source of the first ListBox from code behind. Here is the sample code:

  public partial class MainPage : UserControl
  {
     public MainPage()
     {
        InitializeComponent();

        customerListBoxMain.ItemsSource = PersonDataProvider.GetData();
     }
   }

It's Done..

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文