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

175 lines
5.8 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. public RecipeConfige()
  29. {
  30. InitializeComponent();
  31. ActionManage.GetInstance.CancelRegister("RecipeConfigeViewClose");
  32. ActionManage.GetInstance.Register(new Action(()=> this.Close()), "RecipeConfigeViewClose");
  33. }
  34. private void Button_Click(object sender, RoutedEventArgs e)
  35. {
  36. this.Close();
  37. }
  38. private int GetCurrentIndex(GetPositionDelegate getPosition)
  39. {
  40. int index = -1;
  41. for (int i = 0; i < listview1.Items.Count; ++i)
  42. {
  43. ListViewItem item = GetListViewItem(i);
  44. if (item != null && this.IsMouseOverTarget(item, getPosition))
  45. {
  46. index = i;
  47. break;
  48. }
  49. }
  50. return index;
  51. }
  52. private bool IsMouseOverTarget(Visual target, GetPositionDelegate getPosition)
  53. {
  54. Rect bounds = VisualTreeHelper.GetDescendantBounds(target);
  55. Point mousePos = getPosition((IInputElement)target);
  56. return bounds.Contains(mousePos);
  57. }
  58. delegate Point GetPositionDelegate(IInputElement element);
  59. ListViewItem GetListViewItem(int index)
  60. {
  61. if (listview1.ItemContainerGenerator.Status != GeneratorStatus.ContainersGenerated)
  62. return null;
  63. return listview1.ItemContainerGenerator.ContainerFromIndex(index) as ListViewItem;
  64. }
  65. private void CloneVisual(Border border, MouseButtonEventArgs e)
  66. {
  67. ListBoxItem listBoxItem = new ListBoxItem();
  68. Point ptLeftUp = new Point(0, 0);
  69. ptLeftUp = listview1.PointToScreen(ptLeftUp);
  70. mypopup = new Popup();
  71. double y = e.GetPosition(listview1).Y;
  72. double x = e.GetPosition(listview1).X;
  73. VisualBrush brush = new VisualBrush(border);
  74. Rectangle rect = new Rectangle();
  75. rect.Width = border.ActualWidth;
  76. rect.Height = border.ActualHeight;
  77. rect.Fill = brush;
  78. rect.Opacity = border.Opacity;
  79. border.Opacity = 0.8;
  80. mypopup.Child = rect;
  81. mypopup.AllowsTransparency = true;
  82. mypopup.HorizontalOffset = ptLeftUp.X + x - ((FrameworkElement)ultUE).ActualWidth / 2;
  83. mypopup.VerticalOffset = ptLeftUp.Y + y - ((FrameworkElement)ultUE).ActualHeight / 2;
  84. mypopup.IsOpen = true;
  85. }
  86. private void listview1_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
  87. {
  88. var pos = e.GetPosition(listview1);
  89. HitTestResult result = VisualTreeHelper.HitTest(listview1, pos);
  90. OldItem = Utils.FindVisualParent<ListBoxItem>(result.VisualHit);
  91. if(OldItem != null)
  92. {
  93. if (e.Source is Border)
  94. {
  95. ultUE = (Border)e.Source;
  96. ultUE.CaptureMouse();
  97. ultUE.Visibility = Visibility.Collapsed;
  98. CloneVisual(ultUE, e);
  99. }
  100. }
  101. }
  102. private void listview1_MouseMove(object sender, MouseEventArgs e)
  103. {
  104. Point ptLeftUp = new Point(0, 0);
  105. Point ptRightDown = new Point(this.ActualWidth, this.ActualHeight);
  106. ptLeftUp = listview1.PointToScreen(ptLeftUp);
  107. ptRightDown = listview1.PointToScreen(ptRightDown);
  108. double y = e.GetPosition(listview1).Y;
  109. double x = e.GetPosition(listview1).X;
  110. if (mypopup != null)
  111. {
  112. mypopup.HorizontalOffset = ptLeftUp.X + x - ((FrameworkElement)ultUE).ActualWidth / 2;
  113. mypopup.VerticalOffset = ptLeftUp.Y + y - ((FrameworkElement)ultUE).ActualHeight / 2;
  114. }
  115. }
  116. private void listview1_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
  117. {
  118. if(mypopup != null)
  119. {
  120. mypopup.IsOpen = false;
  121. mypopup = null;
  122. }
  123. if (OldItem == null) return;
  124. var pos = e.GetPosition(listview1);
  125. HitTestResult result = VisualTreeHelper.HitTest(listview1, pos);
  126. if(result == null) return;
  127. var Newitem = Utils.FindVisualParent<ListBoxItem>(result.VisualHit);
  128. if (Newitem == null) return;
  129. if (OldItem == Newitem) return;
  130. int iold = listview1.Items.IndexOf((LocalMaterail)OldItem.DataContext);
  131. int inew = listview1.Items.IndexOf((LocalMaterail)Newitem.DataContext);
  132. RecipeConfigeViewModel.Materails.Move(iold,inew);
  133. }
  134. }
  135. internal static class Utils
  136. {
  137. //根据子元素查找父元素
  138. public static T FindVisualParent<T>(DependencyObject obj) where T : class
  139. {
  140. while (obj != null)
  141. {
  142. if (obj is T)
  143. return obj as T;
  144. obj = VisualTreeHelper.GetParent(obj);
  145. }
  146. return null;
  147. }
  148. }
  149. }