如何在 Silverlight ListBoxDragDropTarget 中查找拖放项目的索引位置

发布于 2024-12-10 10:09:27 字数 200 浏览 2 评论 0原文

我有一个包含在 ListBoxDragDropTarget 中的 silverlight ListBox。我正在监听 DDT 的 Drop 事件,但我不知道如何找到 drop 操作的索引。即我想知道用户在哪个索引点将项目放入我的列表框中。在 UI 上,当我在 ListBox 上拖动时,我可以看到一条线指示我将鼠标悬停在其上的位置,但在放置后,我不知道如何从放置事件中获取放置位置信息。

I have a silverlight ListBox that is contained in a ListBoxDragDropTarget. I am listening to the Drop event of the DDT, but I don't know how to find the index of the drop action. i.e. I want to know at which index spot the user dropped the items into my ListBox. On the UI when I'm dragging over the ListBox, I can see a line indicating the spot that I'm hovering over, but after dropping, I don't know how to get the drop location information from the drop event.

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

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

发布评论

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

评论(1

彻夜缠绵 2024-12-17 10:09:27

给定以下 Xaml:

<Grid x:Name="ListBoxDragDropTarget"
      Background="Gold"
      AllowDrop="True"
      Drop="ListBoxDragDropTarget_Drop">
    <ListBox x:Name="MyListBox" Margin="50">
        <ListBoxItem Content="Item 1" />
        <ListBoxItem Content="Item 2" />
        <ListBoxItem Content="Item 3" />
    </ListBox>
</Grid>

如果您想知道用户在其上放置项目的 ListBoxItem,您可以使用 e.GetPosition 获取鼠标的位置和 VisualTreeHelper.FindElementsInHostCooperatives对于命中测试:

private void ListBoxDragDropTarget_Drop(object sender, DragEventArgs e)
{
    Point position = e.GetPosition(this.ListBoxDragDropTarget);

    var hits = VisualTreeHelper.FindElementsInHostCoordinates(position, this.ListBoxDragDropTarget);

    ListBoxItem dropElement = hits.FirstOrDefault(i => i is ListBoxItem) as ListBoxItem;

    if (dropElement != null)
    {
        // Do something with the dropElement... or if you want the index use ItemContainerGenerator
        int index = this.MyListBox.ItemContainerGenerator.IndexFromContainer(dropElement);
    }
}

Given the following Xaml:

<Grid x:Name="ListBoxDragDropTarget"
      Background="Gold"
      AllowDrop="True"
      Drop="ListBoxDragDropTarget_Drop">
    <ListBox x:Name="MyListBox" Margin="50">
        <ListBoxItem Content="Item 1" />
        <ListBoxItem Content="Item 2" />
        <ListBoxItem Content="Item 3" />
    </ListBox>
</Grid>

If you want to know the ListBoxItem on which the user dropped the items you can use e.GetPosition to get the position of the mouse and VisualTreeHelper.FindElementsInHostCoordinates for hit testing:

private void ListBoxDragDropTarget_Drop(object sender, DragEventArgs e)
{
    Point position = e.GetPosition(this.ListBoxDragDropTarget);

    var hits = VisualTreeHelper.FindElementsInHostCoordinates(position, this.ListBoxDragDropTarget);

    ListBoxItem dropElement = hits.FirstOrDefault(i => i is ListBoxItem) as ListBoxItem;

    if (dropElement != null)
    {
        // Do something with the dropElement... or if you want the index use ItemContainerGenerator
        int index = this.MyListBox.ItemContainerGenerator.IndexFromContainer(dropElement);
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文