|
- using BPASmartClient.MilkWithTea.ViewModel;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows;
- using System.Windows.Controls;
- using System.Windows.Controls.Primitives;
- using System.Windows.Data;
- using System.Windows.Documents;
- using System.Windows.Input;
- using System.Windows.Media;
- using System.Windows.Media.Effects;
- using System.Windows.Media.Imaging;
- using System.Windows.Shapes;
-
- namespace BPASmartClient.MilkWithTea.View
- {
- /// <summary>
- /// RecipeConfige.xaml 的交互逻辑
- /// </summary>
- public partial class RecipeConfige : Window
- {
- double X,Y;
- Border ultUE;
- Popup mypopup;
- ListBoxItem OldItem;
-
- public RecipeConfige()
- {
- InitializeComponent();
- ActionManage.GetInstance.CancelRegister("RecipeConfigeViewClose");
- ActionManage.GetInstance.Register(new Action(()=> this.Close()), "RecipeConfigeViewClose");
- }
-
- private void Button_Click(object sender, RoutedEventArgs e)
- {
- this.Close();
- }
-
-
-
-
-
-
-
- private int GetCurrentIndex(GetPositionDelegate getPosition)
- {
- int index = -1;
- for (int i = 0; i < listview1.Items.Count; ++i)
- {
- ListViewItem item = GetListViewItem(i);
- if (item != null && this.IsMouseOverTarget(item, getPosition))
- {
- index = i;
- break;
- }
- }
- return index;
- }
-
- private bool IsMouseOverTarget(Visual target, GetPositionDelegate getPosition)
- {
- Rect bounds = VisualTreeHelper.GetDescendantBounds(target);
- Point mousePos = getPosition((IInputElement)target);
- return bounds.Contains(mousePos);
- }
-
-
- delegate Point GetPositionDelegate(IInputElement element);
-
- ListViewItem GetListViewItem(int index)
- {
- if (listview1.ItemContainerGenerator.Status != GeneratorStatus.ContainersGenerated)
- return null;
- return listview1.ItemContainerGenerator.ContainerFromIndex(index) as ListViewItem;
- }
-
- private void CloneVisual(Border border, MouseButtonEventArgs e)
- {
- ListBoxItem listBoxItem = new ListBoxItem();
- Point ptLeftUp = new Point(0, 0);
- ptLeftUp = listview1.PointToScreen(ptLeftUp);
- mypopup = new Popup();
- double y = e.GetPosition(listview1).Y;
- double x = e.GetPosition(listview1).X;
-
- VisualBrush brush = new VisualBrush(border);
- Rectangle rect = new Rectangle();
- rect.Width = border.ActualWidth;
- rect.Height = border.ActualHeight;
- rect.Fill = brush;
- rect.Opacity = border.Opacity;
- border.Opacity = 0.8;
- mypopup.Child = rect;
- mypopup.AllowsTransparency = true;
- mypopup.HorizontalOffset = ptLeftUp.X + x - ((FrameworkElement)ultUE).ActualWidth / 2;
- mypopup.VerticalOffset = ptLeftUp.Y + y - ((FrameworkElement)ultUE).ActualHeight / 2;
- mypopup.IsOpen = true;
- }
-
- private void listview1_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
- {
- var pos = e.GetPosition(listview1);
- HitTestResult result = VisualTreeHelper.HitTest(listview1, pos);
- OldItem = Utils.FindVisualParent<ListBoxItem>(result.VisualHit);
- if(OldItem != null)
- {
- if (e.Source is Border)
- {
- ultUE = (Border)e.Source;
- ultUE.CaptureMouse();
- ultUE.Visibility = Visibility.Collapsed;
- CloneVisual(ultUE, e);
- }
- }
- }
-
-
-
- private void listview1_MouseMove(object sender, MouseEventArgs e)
- {
- Point ptLeftUp = new Point(0, 0);
- Point ptRightDown = new Point(this.ActualWidth, this.ActualHeight);
- ptLeftUp = listview1.PointToScreen(ptLeftUp);
- ptRightDown = listview1.PointToScreen(ptRightDown);
- double y = e.GetPosition(listview1).Y;
- double x = e.GetPosition(listview1).X;
- if (mypopup != null)
- {
- mypopup.HorizontalOffset = ptLeftUp.X + x - ((FrameworkElement)ultUE).ActualWidth / 2;
- mypopup.VerticalOffset = ptLeftUp.Y + y - ((FrameworkElement)ultUE).ActualHeight / 2;
- }
- }
-
- private void listview1_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
- {
- if(mypopup != null)
- {
- mypopup.IsOpen = false;
- mypopup = null;
- }
-
- if (OldItem == null) return;
- var pos = e.GetPosition(listview1);
- HitTestResult result = VisualTreeHelper.HitTest(listview1, pos);
- if(result == null) return;
- var Newitem = Utils.FindVisualParent<ListBoxItem>(result.VisualHit);
- if (Newitem == null) return;
- if (OldItem == Newitem) return;
- int iold = listview1.Items.IndexOf((LocalMaterail)OldItem.DataContext);
- int inew = listview1.Items.IndexOf((LocalMaterail)Newitem.DataContext);
- RecipeConfigeViewModel.Materails.Move(iold,inew);
-
- }
- }
-
- internal static class Utils
- {
- //根据子元素查找父元素
- public static T FindVisualParent<T>(DependencyObject obj) where T : class
- {
- while (obj != null)
- {
- if (obj is T)
- return obj as T;
-
- obj = VisualTreeHelper.GetParent(obj);
- }
- return null;
- }
- }
- }
|