WPF中LISTITEM的拖拽
来源:互联网 发布:stc下载软件 编辑:程序博客网 时间:2024/06/07 03:18
此方法较容易实现list中项的拖拽,不过其缺陷是无法将选中的ITEM取出来,SELECTED事件被MOUSELEFTBUTTONDOWN吃掉了。
不罗嗦了,以下是DEMO的源码:
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Windows;using System.Windows.Controls;using System.Windows.Data;using System.Windows.Documents;using System.Windows.Input;using System.Windows.Media;using System.Windows.Media.Imaging;using System.Windows.Navigation;using System.Windows.Shapes;using System.Collections.ObjectModel;using System.Collections;namespace DragTest{ /// <summary> /// MainWindow.xaml 的交互逻辑 /// </summary> public partial class MainWindow : Window { ObservableCollection<string> obv = new ObservableCollection<string>(); public MainWindow() { InitializeComponent(); foreach (TimeZoneInfo tzi in TimeZoneInfo.GetSystemTimeZones()) { lst1.Items.Add(tzi.ToString()); } } ListView dragSource = null; private void lst1_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e) { ListView parent = (ListView)sender; dragSource = parent; object data = GetDataFromListBox(dragSource, e.GetPosition(parent)); if (data != null) { DragDrop.DoDragDrop(parent, data, DragDropEffects.Copy); } } private static object GetDataFromListBox(ListBox source, Point point) { UIElement element = source.InputHitTest(point) as UIElement; if (element != null) { object data = DependencyProperty.UnsetValue; while (data == DependencyProperty.UnsetValue) { data = source.ItemContainerGenerator.ItemFromContainer(element); if (data == DependencyProperty.UnsetValue) { element = VisualTreeHelper.GetParent(element) as UIElement; } if (element == source) { return null; } } if (data != DependencyProperty.UnsetValue) { return data; } } return null; } //listbox1 to listbox2 private void lst2_Drop(object sender, DragEventArgs e) { ListView parent = (ListView)sender; if (parent.Name == lst2.Name) { object data = e.Data.GetData(typeof(string)); ((IList)dragSource.Items).Remove(data); if (!parent.Items.Contains(data)) { parent.Items.Add(data); } } } //listbox2 to listbox1 private void lst1_Drop(object sender, DragEventArgs e) { ListView parent = (ListView)sender; if (parent.Name == lst1.Name) { object data = e.Data.GetData(typeof(string)); ((IList)dragSource.Items).Remove(data); if (!parent.Items.Contains(data)) { parent.Items.Add(data); } } } private void lst2_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e) { ListView parent = (ListView)sender; dragSource = parent; object data = GetDataFromListBox(dragSource, e.GetPosition(parent)); if (data != null) { DragDrop.DoDragDrop(parent, data, DragDropEffects.Move); } } }}
- WPF中LISTITEM的拖拽
- MVVM wpf listitem中绑定list外的命令的方法。
- listView中ListItem Pressed的问题
- Android中listitem的焦点问题
- 设置listitem中每个item的高度
- listView中载入不同的listItem布局
- sencha touch listItem 拖拽处理
- SharePoint中ListItem更新的一个需要注意的地方
- 在已有数据的listbox中新增一行listitem
- ListItem的删除问题
- 自定义ListView实现拖拽ListItem项交换位置
- 多线程ListView的listItem 赋值
- 在VB中任意设置ListView控件的ListItem的背景颜色
- Android布局中ScrollView与ListView的冲突的最简单方法(listItem.measure(0, 0))
- Android布局中ScrollView与ListView的冲突的最简单方法(listItem.measure(0, 0))
- andriod中ListView, listItem布局和convertView的缓存与重用详解
- android listview中按钮点击怎样获取到那条选中listItem的position
- WPF 中 的多线程
- 面试和笔试
- 如何隐藏nginx版本号来提高安全性
- 变态hello word(带注释)
- 自动调用webservice_axis
- 传值还是传地址
- WPF中LISTITEM的拖拽
- USACO算法系列十九——fence4
- C/C++面试题(一)
- 系统、软件资源收藏
- [转]eclipse3.6(helios)配置Tomcat后无法访问http://localhost:8080/
- java压缩文件
- 大学多的只是一段特殊旅程
- 20个Nginx Web服务器最佳安全实践
- USACO算法系列二十——heritage