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

290 regels
10 KiB

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