终端一体化运控平台
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.
 
 
 

348 lines
12 KiB

  1. using BPA.Models;
  2. using BPA.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.Hide();
  44. }), "LocalMenuClose");
  45. }
  46. private void Button_Click(object sender, RoutedEventArgs e)
  47. {
  48. this.Hide();
  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(new int[] { i, j }, "PotActionStep");
  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. private void Materials_SelectionChanged(object sender, SelectionChangedEventArgs e)
  202. {
  203. string str = Materials.SelectedValue?.ToString();
  204. if (str != string.Empty)
  205. {
  206. ActionManage.GetInstance.Send("LoacMenuSelectMaterial", str);
  207. }
  208. }
  209. /// <summary>
  210. /// 移动效果
  211. /// </summary>
  212. /// <param name="boxItem"></param>
  213. private void MoveListBoxStyle(MouseEventArgs e, bool isBool)
  214. {
  215. try
  216. {
  217. if (isBool)//为真,根据鼠标位置设置行透明度和显示状态
  218. {
  219. //移动到某行减轻某行 暗黑
  220. //foreach (ListBoxItem item in Utils.FindVisualChildren<ListBoxItem>(listview1))
  221. //{
  222. // if (item != ChildListBoxItem)//这就是其他控件
  223. // {
  224. // double item_width = item.ActualWidth; //当前行宽
  225. // double item_height = item.ActualHeight; //当前行高
  226. // double item_x = e.GetPosition(item).X; //鼠标相对当前行X位移
  227. // double item_y = e.GetPosition(item).Y; //鼠标相对当前行Y位移
  228. // if (item_y <= item_height && item_y > 0 && item_x > 0 && item_x <= item_width)//鼠标进入哪一行,则将那一行变灰
  229. // {
  230. // item.Opacity = 0.5;
  231. // item.Background = Brushes.AliceBlue;
  232. // lao_index = LocalMenuViewModel.PotActionStep.IndexOf(item.Content as PotActions);
  233. // new_index = LocalMenuViewModel.PotActionStep.IndexOf(ChildListBoxItem.Content as PotActions);
  234. // //LocalMenuViewModel.PotActionStep.Move(lao_index, new_index);
  235. // }
  236. // else //鼠标没在哪一行,则保持原状
  237. // {
  238. // item.Opacity = 1;
  239. // }
  240. // }
  241. // else
  242. // {
  243. // item.Visibility = Visibility.Hidden;
  244. // }
  245. //}
  246. }
  247. else//为假 恢复所有行透明度和显示状态
  248. {
  249. //LocalMenuViewModel.PotActionStep.Move(lao_index, new_index);
  250. //移动到某行减轻某行 暗黑
  251. foreach (ListBoxItem item in Utils.FindVisualChildren<ListBoxItem>(listview1))
  252. {
  253. item.Background = Brushes.Transparent;
  254. item.Opacity = 1;
  255. item.Visibility = Visibility.Visible;
  256. }
  257. }
  258. }
  259. catch (Exception ex)
  260. {
  261. }
  262. }
  263. }
  264. internal static class Utils
  265. {
  266. //根据子元素查找父元素
  267. public static T FindVisualParent<T>(DependencyObject obj) where T : class
  268. {
  269. while (obj != null)
  270. {
  271. if (obj is T)
  272. return obj as T;
  273. obj = VisualTreeHelper.GetParent(obj);
  274. }
  275. return null;
  276. }
  277. /// <summary>
  278. /// 查询子控件
  279. /// </summary>
  280. /// <typeparam name="T"></typeparam>
  281. /// <param name="obj"></param>
  282. /// <returns></returns>
  283. public static IEnumerable<T> FindVisualChildren<T>(DependencyObject depObj) where T : DependencyObject
  284. {
  285. if (depObj != null)
  286. {
  287. for (int i = 0; i < VisualTreeHelper.GetChildrenCount(depObj); i++)
  288. {
  289. DependencyObject child = VisualTreeHelper.GetChild(depObj, i);
  290. if (child != null && child is T)
  291. {
  292. yield return (T)child;
  293. }
  294. foreach (T childOfChild in FindVisualChildren<T>(child))
  295. {
  296. yield return childOfChild;
  297. }
  298. }
  299. }
  300. }
  301. }
  302. }