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

436 lines
15 KiB

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