查找ListBox里面ListBoxItem

本文介绍了一种方法,用于从绑定的数据集中获取ListBoxItem容器。通过这种方法,可以更有效地操作和利用ListBox中选中的项。
 ListBoxItem item = lbDepartment.ItemContainerGenerator.ContainerFromItem(dc) as ListBoxItem;

dc是绑定ListBox选中的模型

在不同的编程环境中,实现将 `ListBox` 中的元素拖拽到指定行的方法有所不同,以下分别给出在 Windows Forms(C#)和 WPFC#)中的实现示例。 ### Windows Forms(C#) ```csharp using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace ListBoxDragDrop { public partial class Form1 : Form { private int draggedIndex = -1; public Form1() { InitializeComponent(); listBox1.AllowDrop = true; listBox1.MouseDown += ListBox1_MouseDown; listBox1.DragOver += ListBox1_DragOver; listBox1.DragDrop += ListBox1_DragDrop; } private void ListBox1_MouseDown(object sender, MouseEventArgs e) { draggedIndex = listBox1.IndexFromPoint(e.Location); if (draggedIndex != ListBox.NoMatches) { listBox1.DoDragDrop(listBox1.Items[draggedIndex], DragDropEffects.Move); } } private void ListBox1_DragOver(object sender, DragEventArgs e) { e.Effect = DragDropEffects.Move; } private void ListBox1_DragDrop(object sender, DragEventArgs e) { Point targetPoint = listBox1.PointToClient(new Point(e.X, e.Y)); int targetIndex = listBox1.IndexFromPoint(targetPoint); if (targetIndex == ListBox.NoMatches) { targetIndex = listBox1.Items.Count; } object draggedItem = e.Data.GetData(typeof(object)); listBox1.Items.RemoveAt(draggedIndex); listBox1.Items.Insert(targetIndex, draggedItem); } } } ``` 在上述代码中,首先在 `MouseDown` 事件中记录被拖拽元素的索引,并开始拖放操作。在 `DragOver` 事件中设置拖放效果为移动。在 `DragDrop` 事件中,获取目标位置的索引,移除被拖拽的元素并插入到目标位置。 ### WPFC#) ```xml <Window x:Class="WpfListBoxDragDrop.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" Height="350" Width="525"> <Grid> <ListBox x:Name="listBox1" AllowDrop="True" PreviewMouseLeftButtonDown="ListBox1_PreviewMouseLeftButtonDown" DragOver="ListBox1_DragOver" Drop="ListBox1_Drop"> <ListBox.ItemContainerStyle> <Style TargetType="{x:Type ListBoxItem}"> <Setter Property="AllowDrop" Value="True"/> </Style> </ListBox.ItemContainerStyle> </ListBox> </Grid> </Window> ``` ```csharp using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using System.Windows.Input; namespace WpfListBoxDragDrop { public partial class MainWindow : Window { private object draggedItem; public MainWindow() { InitializeComponent(); for (int i = 0; i < 10; i++) { listBox1.Items.Add($"Item {i}"); } } private void ListBox1_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e) { ListBoxItem item = FindAnchestor<ListBoxItem>((DependencyObject)e.OriginalSource); if (item != null) { draggedItem = item.DataContext; DragDrop.DoDragDrop(item, draggedItem, DragDropEffects.Move); } } private void ListBox1_DragOver(object sender, DragEventArgs e) { e.Effects = DragDropEffects.Move; e.Handled = true; } private void ListBox1_Drop(object sender, DragEventArgs e) { ListBox listBox = sender as ListBox; Point targetPoint = e.GetPosition(listBox); ListBoxItem targetItem = FindAnchestor<ListBoxItem>((DependencyObject)e.OriginalSource); int targetIndex = listBox.ItemContainerGenerator.IndexFromContainer(targetItem); if (targetIndex == -1) { targetIndex = listBox.Items.Count; } listBox.Items.Remove(draggedItem); listBox.Items.Insert(targetIndex, draggedItem); } private static T FindAnchestor<T>(DependencyObject current) where T : DependencyObject { do { if (current is T) { return (T)current; } current = System.Windows.Media.VisualTreeHelper.GetParent(current); } while (current != null); return null; } } } ``` 在 WPF 中,通过 `PreviewMouseLeftButtonDown` 事件开始拖放操作,`DragOver` 事件设置拖放效果,`Drop` 事件处理元素的移动。`FindAnchestor` 方法用于查找指定类型的父元素。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值