终端一体化运控平台
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

339 lines
12 KiB

  1. using BPA.Models;
  2. using BPASmartClient.Helper;
  3. using BPASmartClient.MorkF.ViewModel;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows;
  10. using System.Windows.Controls;
  11. using System.Windows.Controls.Primitives;
  12. using System.Windows.Data;
  13. using System.Windows.Documents;
  14. using System.Windows.Input;
  15. using System.Windows.Media;
  16. using System.Windows.Media.Imaging;
  17. using System.Windows.Shapes;
  18. namespace BPASmartClient.MorkF.View
  19. {
  20. /// <summary>
  21. /// LocalMenu.xaml 的交互逻辑
  22. /// </summary>
  23. public partial class LocalMenu : Window
  24. {
  25. double X, Y;
  26. Border ultUE;
  27. Popup mypopup;
  28. ListBoxItem OldItem;
  29. ListBoxItem new_Item;
  30. bool isDown = false;
  31. int lao_index;
  32. int new_index;
  33. /// <summary>
  34. /// 当前拖拽子控件Item
  35. /// </summary>
  36. private ListBoxItem ChildListBoxItem;
  37. public LocalMenu()
  38. {
  39. InitializeComponent();
  40. ActionManage.GetInstance.CancelRegister ("LocalMenuClose");
  41. ActionManage.GetInstance.Register(new Action(() =>
  42. {
  43. this.Close();
  44. }), "LocalMenuClose");
  45. }
  46. private void Button_Click(object sender, RoutedEventArgs e)
  47. {
  48. this.Close();
  49. }
  50. private int GetCurrentIndex(GetPositionDelegate getPosition)
  51. {
  52. int index = -1;
  53. for (int i = 0; i < listview1.Items.Count; ++i)
  54. {
  55. ListBoxItem item = GetListViewItem(i);
  56. if (item != null && this.IsMouseOverTarget(item, getPosition))
  57. {
  58. index = i;
  59. break;
  60. }
  61. }
  62. return index;
  63. }
  64. private bool IsMouseOverTarget(Visual target, GetPositionDelegate getPosition)
  65. {
  66. Rect bounds = VisualTreeHelper.GetDescendantBounds(target);
  67. Point mousePos = getPosition((IInputElement)target);
  68. return bounds.Contains(mousePos);
  69. }
  70. delegate Point GetPositionDelegate(IInputElement element);
  71. ListBoxItem GetListViewItem(int index)
  72. {
  73. if (listview1.ItemContainerGenerator.Status != GeneratorStatus.ContainersGenerated)
  74. return null;
  75. return listview1.ItemContainerGenerator.ContainerFromIndex(index) as ListBoxItem;
  76. }
  77. private void CloneVisual(Border border, MouseButtonEventArgs e)
  78. {
  79. ListBoxItem listBoxItem = new ListBoxItem();
  80. Point ptLeftUp = new Point(0, 0);
  81. ptLeftUp = listview1.PointToScreen(ptLeftUp);
  82. mypopup = new Popup();
  83. double y = e.GetPosition(listview1).Y;
  84. double x = e.GetPosition(listview1).X;
  85. VisualBrush brush = new VisualBrush(border);
  86. Rectangle rect = new Rectangle();
  87. rect.Width = border.ActualWidth;
  88. rect.Height = border.ActualHeight;
  89. rect.Fill = brush;
  90. rect.Opacity = border.Opacity;
  91. border.Opacity = 0.8;
  92. mypopup.Child = rect;
  93. mypopup.AllowsTransparency = true;
  94. mypopup.HorizontalOffset = ptLeftUp.X + x - ((FrameworkElement)ultUE).ActualWidth / 2;
  95. mypopup.VerticalOffset = ptLeftUp.Y + y - ((FrameworkElement)ultUE).ActualHeight / 2;
  96. mypopup.IsOpen = true;
  97. }
  98. private void listview1_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
  99. {
  100. try
  101. {
  102. if (isDown)
  103. {
  104. isDown = false;
  105. return;
  106. }
  107. var pos = e.GetPosition(listview1);
  108. HitTestResult result = VisualTreeHelper.HitTest(listview1, pos);
  109. if (result == null) return;
  110. OldItem = Utils.FindVisualParent<ListBoxItem>(result.VisualHit);
  111. if (OldItem != null)
  112. {
  113. if (e.Source is Border)
  114. {
  115. ultUE = (Border)e.Source;
  116. ultUE.CaptureMouse();
  117. ChildListBoxItem = Utils.FindVisualParent<ListBoxItem>(VisualTreeHelper.HitTest(ultUE, e.GetPosition(ultUE)).VisualHit);
  118. CloneVisual(ultUE, e);
  119. isDown = true;
  120. }
  121. }
  122. }
  123. catch (Exception)
  124. {
  125. throw;
  126. }
  127. }
  128. private void listview1_MouseMove(object sender, MouseEventArgs e)
  129. {
  130. if (sender is ListBox listview&& e.LeftButton == MouseButtonState.Pressed&& listview.SelectedItem != null)
  131. {
  132. var pos = e.GetPosition(listview1);
  133. HitTestResult result = VisualTreeHelper.HitTest(listview1, pos);
  134. if (result == null)
  135. {
  136. return;
  137. }
  138. var listBoxItem = Utils.FindVisualParent<ListBoxItem>(result.VisualHit);
  139. if (listBoxItem == null || listBoxItem.Content != listview1.SelectedItem)
  140. {
  141. return;
  142. }
  143. DataObject dataObj = new DataObject(listBoxItem.Content as PotActions);
  144. DragDrop.DoDragDrop(listview1, dataObj, DragDropEffects.Move);
  145. ActionManage.GetInstance.Send("SortFryTime");
  146. }
  147. }
  148. private void listview1_Drop(object sender, DragEventArgs e)
  149. {
  150. var pos = e.GetPosition(listview1);
  151. var result = VisualTreeHelper.HitTest(listview1, pos);
  152. if (result == null)
  153. {
  154. return;
  155. }
  156. //查找元数据
  157. var sourcePerson = e.Data.GetData(typeof(PotActions)) as PotActions;
  158. if (sourcePerson == null)
  159. {
  160. return;
  161. }
  162. //查找目标数据
  163. var listBoxItem = Utils.FindVisualParent<ListBoxItem>(result.VisualHit);
  164. if (listBoxItem == null)
  165. {
  166. return;
  167. }
  168. var targetPerson = listBoxItem.Content as PotActions;
  169. if (ReferenceEquals(targetPerson, sourcePerson))
  170. {
  171. return;
  172. }
  173. int i = listview1.Items.IndexOf(sourcePerson);
  174. int j = listview1.Items.IndexOf(targetPerson);
  175. ActionManage.GetInstance.Send("PotActionStep", new int[] { i, j });
  176. //LocalMenuViewModel.PotActionStep.RemoveAt(listview1.Items.IndexOf(sourcePerson));
  177. //LocalMenuViewModel.PotActionStep.Insert(listview1.Items.IndexOf(targetPerson)+1, sourcePerson);
  178. //targetPerson.FryTime = listview1.Items.IndexOf(targetPerson) -1;
  179. //LocalMenuViewModel.PotActionStep.Move(listview1.Items.IndexOf(sourcePerson), listview1.Items.IndexOf(targetPerson));
  180. }
  181. private void listview1_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
  182. {
  183. ////鼠标未按下返回
  184. //if (!isDown) return;
  185. //isDown = false;
  186. //var pos = e.GetPosition(listview1);
  187. //HitTestResult result = VisualTreeHelper.HitTest(listview1, pos);
  188. //if (result == null) return;
  189. //new_Item = Utils.FindVisualParent<ListBoxItem>(result.VisualHit);
  190. //if (mypopup != null)
  191. //{
  192. // mypopup.IsOpen = false;
  193. // mypopup = null;
  194. //}
  195. //MoveListBoxStyle(e, false);
  196. }
  197. private void Window_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
  198. {
  199. this.DragMove();
  200. }
  201. /// <summary>
  202. /// 移动效果
  203. /// </summary>
  204. /// <param name="boxItem"></param>
  205. private void MoveListBoxStyle(MouseEventArgs e, bool isBool)
  206. {
  207. try
  208. {
  209. if (isBool)//为真,根据鼠标位置设置行透明度和显示状态
  210. {
  211. //移动到某行减轻某行 暗黑
  212. //foreach (ListBoxItem item in Utils.FindVisualChildren<ListBoxItem>(listview1))
  213. //{
  214. // if (item != ChildListBoxItem)//这就是其他控件
  215. // {
  216. // double item_width = item.ActualWidth; //当前行宽
  217. // double item_height = item.ActualHeight; //当前行高
  218. // double item_x = e.GetPosition(item).X; //鼠标相对当前行X位移
  219. // double item_y = e.GetPosition(item).Y; //鼠标相对当前行Y位移
  220. // if (item_y <= item_height && item_y > 0 && item_x > 0 && item_x <= item_width)//鼠标进入哪一行,则将那一行变灰
  221. // {
  222. // item.Opacity = 0.5;
  223. // item.Background = Brushes.AliceBlue;
  224. // lao_index = LocalMenuViewModel.PotActionStep.IndexOf(item.Content as PotActions);
  225. // new_index = LocalMenuViewModel.PotActionStep.IndexOf(ChildListBoxItem.Content as PotActions);
  226. // //LocalMenuViewModel.PotActionStep.Move(lao_index, new_index);
  227. // }
  228. // else //鼠标没在哪一行,则保持原状
  229. // {
  230. // item.Opacity = 1;
  231. // }
  232. // }
  233. // else
  234. // {
  235. // item.Visibility = Visibility.Hidden;
  236. // }
  237. //}
  238. }
  239. else//为假 恢复所有行透明度和显示状态
  240. {
  241. //LocalMenuViewModel.PotActionStep.Move(lao_index, new_index);
  242. //移动到某行减轻某行 暗黑
  243. foreach (ListBoxItem item in Utils.FindVisualChildren<ListBoxItem>(listview1))
  244. {
  245. item.Background = Brushes.Transparent;
  246. item.Opacity = 1;
  247. item.Visibility = Visibility.Visible;
  248. }
  249. }
  250. }
  251. catch (Exception ex)
  252. {
  253. }
  254. }
  255. }
  256. internal static class Utils
  257. {
  258. //根据子元素查找父元素
  259. public static T FindVisualParent<T>(DependencyObject obj) where T : class
  260. {
  261. while (obj != null)
  262. {
  263. if (obj is T)
  264. return obj as T;
  265. obj = VisualTreeHelper.GetParent(obj);
  266. }
  267. return null;
  268. }
  269. /// <summary>
  270. /// 查询子控件
  271. /// </summary>
  272. /// <typeparam name="T"></typeparam>
  273. /// <param name="obj"></param>
  274. /// <returns></returns>
  275. public static IEnumerable<T> FindVisualChildren<T>(DependencyObject depObj) where T : DependencyObject
  276. {
  277. if (depObj != null)
  278. {
  279. for (int i = 0; i < VisualTreeHelper.GetChildrenCount(depObj); i++)
  280. {
  281. DependencyObject child = VisualTreeHelper.GetChild(depObj, i);
  282. if (child != null && child is T)
  283. {
  284. yield return (T)child;
  285. }
  286. foreach (T childOfChild in FindVisualChildren<T>(child))
  287. {
  288. yield return childOfChild;
  289. }
  290. }
  291. }
  292. }
  293. }
  294. }