终端一体化运控平台
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.
 
 
 

358 lignes
12 KiB

  1. using Microsoft.Toolkit.Mvvm.ComponentModel;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Collections.ObjectModel;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. using System.Windows;
  9. using System.Windows.Controls;
  10. using System.Windows.Controls.Primitives;
  11. using System.Windows.Data;
  12. using System.Windows.Documents;
  13. using System.Windows.Input;
  14. using System.Windows.Media;
  15. using System.Windows.Media.Effects;
  16. using System.Windows.Media.Imaging;
  17. using System.Windows.Navigation;
  18. using System.Windows.Shapes;
  19. namespace WPFDemo
  20. {
  21. /// <summary>
  22. /// fyf 创建测试案例
  23. /// TheListBox.xaml 的交互逻辑
  24. /// </summary>
  25. public partial class TheListBox : UserControl
  26. {
  27. /// <summary>
  28. /// 数据model
  29. /// </summary>
  30. public ListBoxDataModel TheListBoxModel = new ListBoxDataModel();
  31. public TheListBox()
  32. {
  33. InitializeComponent();
  34. this.DataContext = TheListBoxModel;
  35. TheListBoxModel.ViewItems.Add(new ItemModel { Name = "张三", Ph = "125486545" });
  36. TheListBoxModel.ViewItems.Add(new ItemModel { Name = "李四", Ph = "125486545" });
  37. TheListBoxModel.ViewItems.Add(new ItemModel { Name = "王麻子", Ph = "125486545" });
  38. TheListBoxModel.ViewItems.Add(new ItemModel { Name = "二货", Ph = "125486545" });
  39. TheListBoxModel.ViewItems.Add(new ItemModel { Name = "张三1", Ph = "125486545" });
  40. TheListBoxModel.ViewItems.Add(new ItemModel { Name = "李四2", Ph = "125486545" });
  41. TheListBoxModel.ViewItems.Add(new ItemModel { Name = "王麻子3", Ph = "125486545" });
  42. }
  43. #region 移动事件
  44. /// <summary>
  45. /// 当前拖动子控件流
  46. /// </summary>
  47. private UIElement ChildElement;
  48. /// <summary>
  49. /// 当前拖拽子控件Item
  50. /// </summary>
  51. private ListBoxItem ChildListBoxItem;
  52. /// <summary>
  53. /// 是否已经按下
  54. /// </summary>
  55. private bool isDown = false;
  56. /// <summary>
  57. /// 当前拖动的Pop窗体
  58. /// </summary>
  59. private Popup DropPopup = null;
  60. /// <summary>
  61. /// 鼠标按下事件
  62. /// </summary>
  63. /// <param name="sender"></param>
  64. /// <param name="e"></param>
  65. private void Border_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
  66. {
  67. try
  68. {
  69. //isDown变量是防止多次操作。
  70. if (isDown)
  71. {
  72. isDown = false;
  73. return;
  74. }
  75. ChildElement = (UIElement)sender;
  76. ChildElement.CaptureMouse();//设置了鼠标捕获,这样它可以不受到其它控件的影响。
  77. ChildListBoxItem = Utils.FindVisualParent<ListBoxItem>(VisualTreeHelper.HitTest(ChildElement, e.GetPosition(ChildElement)).VisualHit);
  78. //创建一个Pop,表明拖拽开始
  79. CreatePopup(ChildElement, e);
  80. isDown = true;
  81. }
  82. catch (Exception ex)
  83. {
  84. }
  85. }
  86. /// <summary>
  87. /// 鼠标抬起事件
  88. /// </summary>
  89. /// <param name="sender"></param>
  90. /// <param name="e"></param>
  91. private void lisbox_PreviewMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
  92. {
  93. try
  94. {
  95. //鼠标未按下返回
  96. if (!isDown) return;
  97. isDown = false;
  98. //关闭Pop窗体
  99. if (this.DropPopup != null)
  100. {
  101. this.DropPopup.IsOpen = false;
  102. this.DropPopup.Child = null;
  103. this.DropPopup = null;
  104. }
  105. //蒙层关闭,表明结束拖拽
  106. MoveListBoxStyle(null, false);
  107. //当控件具有鼠标捕获的话,则释放该捕获。
  108. ChildElement.ReleaseMouseCapture();
  109. }
  110. catch (Exception ex)
  111. {
  112. }
  113. }
  114. /// <summary>
  115. /// 鼠标移动事件
  116. /// </summary>
  117. /// <param name="sender"></param>
  118. /// <param name="e"></param>
  119. private void ListView_PreviewMouseMove(object sender, MouseEventArgs e)
  120. {
  121. try
  122. {
  123. if (isDown == false) return;
  124. if (e.LeftButton != MouseButtonState.Pressed)
  125. lisbox_PreviewMouseLeftButtonUp(null, null);
  126. Point ptLeftUp = new Point(0, 0);
  127. Point ptRightDown = new Point(this.ActualWidth, this.ActualHeight);
  128. ptLeftUp = this.PointToScreen(ptLeftUp);
  129. ptRightDown = this.PointToScreen(ptRightDown);
  130. double y = e.GetPosition(this).Y;
  131. double x = e.GetPosition(this).X;
  132. if (DropPopup != null)//下面两句是设置Popup控件的位置除以2是想让鼠标在它的中心
  133. {
  134. DropPopup.HorizontalOffset = ptLeftUp.X + x - ((FrameworkElement)ChildElement).ActualWidth / 2;
  135. DropPopup.VerticalOffset = ptLeftUp.Y + y - ((FrameworkElement)ChildElement).ActualHeight / 2;
  136. }
  137. //蒙层打开,表明拖拽开始,设置透明度和显示状态
  138. MoveListBoxStyle(e, true);
  139. }
  140. catch (Exception ex)
  141. {
  142. }
  143. }
  144. /// <summary>
  145. /// 移动效果
  146. /// </summary>
  147. /// <param name="boxItem"></param>
  148. private void MoveListBoxStyle(MouseEventArgs e, bool isBool)
  149. {
  150. try
  151. {
  152. if (isBool)//为真,根据鼠标位置设置行透明度和显示状态
  153. {
  154. //移动到某行减轻某行 暗黑
  155. foreach (ListBoxItem item in Utils.FindVisualChildren<ListBoxItem>(listView))
  156. {
  157. if (item != ChildListBoxItem)//这就是其他控件
  158. {
  159. double item_width = item.ActualWidth; //当前行宽
  160. double item_height = item.ActualHeight; //当前行高
  161. double item_x = e.GetPosition(item).X; //鼠标相对当前行X位移
  162. double item_y = e.GetPosition(item).Y; //鼠标相对当前行Y位移
  163. if (item_y <= item_height && item_y > 0 && item_x > 0 && item_x <= item_width)//鼠标进入哪一行,则将那一行变灰
  164. {
  165. item.Opacity = 0.5;
  166. int lao_index = TheListBoxModel.ViewItems.IndexOf(item.Content as ItemModel);
  167. int new_index = TheListBoxModel.ViewItems.IndexOf(ChildListBoxItem.Content as ItemModel);
  168. TheListBoxModel.ViewItems.Move(lao_index, new_index);
  169. }
  170. else //鼠标没在哪一行,则保持原状
  171. {
  172. item.Opacity = 1;
  173. }
  174. }
  175. else
  176. {
  177. item.Visibility = Visibility.Hidden;
  178. }
  179. }
  180. }
  181. else//为假 恢复所有行透明度和显示状态
  182. {
  183. //移动到某行减轻某行 暗黑
  184. foreach (ListBoxItem item in Utils.FindVisualChildren<ListBoxItem>(listView))
  185. {
  186. item.Opacity = 1;
  187. item.Visibility = Visibility.Visible;
  188. }
  189. }
  190. }
  191. catch (Exception ex)
  192. {
  193. }
  194. }
  195. /// <summary>
  196. /// 创建浮动窗口
  197. /// </summary>
  198. /// <param name="dragElement"></param>
  199. /// <param name="e"></param>
  200. private void CreatePopup(Visual dragElement, MouseButtonEventArgs e)
  201. {
  202. //使用PointToScreen函数可以将点转换为屏幕坐标
  203. //首先获取当前窗体的左上角和右下角两点的坐标
  204. Point ptLeftUp = new Point(0, 0);
  205. //转换获取到这个窗口相对于屏幕两个坐标
  206. ptLeftUp = this.PointToScreen(ptLeftUp);
  207. //获取myGrid的实际宽高,主
  208. double y = e.GetPosition(this).Y;
  209. double x = e.GetPosition(this).X;
  210. //拖拽Popup框
  211. this.DropPopup = new Popup();
  212. Border border = new Border();
  213. border.Margin = new Thickness(0, 0, 8, 8);
  214. DropShadowEffect effect = new DropShadowEffect();
  215. effect.Opacity = 1;
  216. effect.ShadowDepth = -14;
  217. effect.BlurRadius = 9;
  218. effect.Color = Color.FromArgb(100, 0, 0, 0);
  219. border.Effect = effect;
  220. //矩阵框
  221. Rectangle r = new Rectangle();
  222. r.Width = ((FrameworkElement)dragElement).ActualWidth;
  223. r.Height = ((FrameworkElement)dragElement).ActualHeight;
  224. r.Fill = new VisualBrush(dragElement);
  225. border.Child = r;
  226. this.DropPopup.Child = border;
  227. DropPopup.AllowsTransparency = true;
  228. DropPopup.HorizontalOffset = ptLeftUp.X + x - ((FrameworkElement)dragElement).ActualWidth / 2;
  229. DropPopup.VerticalOffset = ptLeftUp.Y + y - ((FrameworkElement)dragElement).ActualHeight / 2;
  230. this.DropPopup.IsOpen = true;
  231. }
  232. #endregion
  233. }
  234. /// <summary>
  235. /// 当前项数据Model
  236. /// </summary>
  237. public class ListBoxDataModel : ObservableObject
  238. {
  239. private ObservableCollection<ItemModel> _ViewItems;
  240. public ObservableCollection<ItemModel> ViewItems
  241. {
  242. get
  243. {
  244. return _ViewItems;
  245. }
  246. set
  247. {
  248. _ViewItems = value;
  249. OnPropertyChanged("ViewItems");
  250. }
  251. }
  252. public ListBoxDataModel()
  253. {
  254. ViewItems = new ObservableCollection<ItemModel>();
  255. }
  256. }
  257. /// <summary>
  258. /// 行数据Model
  259. /// </summary>
  260. public class ItemModel : ObservableObject
  261. {
  262. private string _Name;
  263. public string Name
  264. {
  265. get
  266. {
  267. return _Name;
  268. }
  269. set
  270. {
  271. _Name = value;
  272. OnPropertyChanged("Name");
  273. }
  274. }
  275. private string _Ph;
  276. public string Ph
  277. {
  278. get
  279. {
  280. return _Ph;
  281. }
  282. set
  283. {
  284. _Ph = value;
  285. OnPropertyChanged("Ph");
  286. }
  287. }
  288. }
  289. /// <summary>
  290. /// 帮助类
  291. /// </summary>
  292. internal static class Utils
  293. {
  294. /// <summary>
  295. /// 根据子元素查找父元素
  296. /// </summary>
  297. /// <typeparam name="T"></typeparam>
  298. /// <param name="obj"></param>
  299. /// <returns></returns>
  300. public static T FindVisualParent<T>(DependencyObject obj) where T : class
  301. {
  302. while (obj != null)
  303. {
  304. if (obj is T)
  305. return obj as T;
  306. obj = VisualTreeHelper.GetParent(obj);
  307. }
  308. return null;
  309. }
  310. /// <summary>
  311. /// 查询子控件
  312. /// </summary>
  313. /// <typeparam name="T"></typeparam>
  314. /// <param name="obj"></param>
  315. /// <returns></returns>
  316. public static IEnumerable<T> FindVisualChildren<T>(DependencyObject depObj) where T : DependencyObject
  317. {
  318. if (depObj != null)
  319. {
  320. for (int i = 0; i < VisualTreeHelper.GetChildrenCount(depObj); i++)
  321. {
  322. DependencyObject child = VisualTreeHelper.GetChild(depObj, i);
  323. if (child != null && child is T)
  324. {
  325. yield return (T)child;
  326. }
  327. foreach (T childOfChild in FindVisualChildren<T>(child))
  328. {
  329. yield return childOfChild;
  330. }
  331. }
  332. }
  333. }
  334. }
  335. }