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
{
///
/// RecipeConfige.xaml 的交互逻辑
///
public partial class RecipeConfige : Window
{
double X,Y;
Border ultUE;
Popup mypopup;
ListBoxItem OldItem;
bool isDown = false;
///
/// 当前拖拽子控件Item
///
private ListBoxItem ChildListBoxItem;
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)
{
try
{
if (isDown)
{
isDown = false;
return;
}
var pos = e.GetPosition(listview1);
HitTestResult result = VisualTreeHelper.HitTest(listview1, pos);
if (result == null) return;
OldItem = Utils.FindVisualParent(result.VisualHit);
if (OldItem != null)
{
if (e.Source is Border)
{
ultUE = (Border)e.Source;
ultUE.CaptureMouse();
ChildListBoxItem = Utils.FindVisualParent(VisualTreeHelper.HitTest(ultUE, e.GetPosition(ultUE)).VisualHit);
CloneVisual(ultUE, e);
isDown = true;
}
}
}
catch (Exception)
{
throw;
}
}
private void listview1_MouseMove(object sender, MouseEventArgs e)
{
if (isDown == false) return;
if (e.LeftButton != MouseButtonState.Pressed)
listview1_MouseLeftButtonUp(null,null);
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;
}
MoveListBoxStyle(e, true);
}
private void listview1_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
//鼠标未按下返回
if (!isDown) return;
isDown = false;
if (mypopup != null)
{
mypopup.IsOpen = false;
mypopup = null;
}
MoveListBoxStyle(e, false);
//if (OldItem == null) return;
//var pos = e.GetPosition(listview1);
//HitTestResult result = VisualTreeHelper.HitTest(listview1, pos);
//if(result == null) return;
//var Newitem = Utils.FindVisualParent(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);
}
///
/// 移动效果
///
///
private void MoveListBoxStyle(MouseEventArgs e, bool isBool)
{
try
{
if (isBool)//为真,根据鼠标位置设置行透明度和显示状态
{
//移动到某行减轻某行 暗黑
foreach (ListBoxItem item in Utils.FindVisualChildren(listview1))
{
if (item != ChildListBoxItem)//这就是其他控件
{
double item_width = item.ActualWidth; //当前行宽
double item_height = item.ActualHeight; //当前行高
double item_x = e.GetPosition(item).X; //鼠标相对当前行X位移
double item_y = e.GetPosition(item).Y; //鼠标相对当前行Y位移
if (item_y <= item_height && item_y > 0 && item_x > 0 && item_x <= item_width)//鼠标进入哪一行,则将那一行变灰
{
item.Opacity = 0.5;
int lao_index = RecipeConfigeViewModel.Materails.IndexOf(item.Content as LocalMaterail);
int new_index = RecipeConfigeViewModel.Materails.IndexOf(ChildListBoxItem.Content as LocalMaterail);
RecipeConfigeViewModel.Materails.Move(lao_index, new_index);
}
else //鼠标没在哪一行,则保持原状
{
item.Opacity = 1;
}
}
else
{
item.Visibility = Visibility.Hidden;
}
}
}
else//为假 恢复所有行透明度和显示状态
{
//移动到某行减轻某行 暗黑
foreach (ListBoxItem item in Utils.FindVisualChildren(listview1))
{
item.Opacity = 1;
item.Visibility = Visibility.Visible;
}
}
}
catch (Exception ex)
{
}
}
}
internal static class Utils
{
//根据子元素查找父元素
public static T FindVisualParent(DependencyObject obj) where T : class
{
while (obj != null)
{
if (obj is T)
return obj as T;
obj = VisualTreeHelper.GetParent(obj);
}
return null;
}
///
/// 查询子控件
///
///
///
///
public static IEnumerable FindVisualChildren(DependencyObject depObj) where T : DependencyObject
{
if (depObj != null)
{
for (int i = 0; i < VisualTreeHelper.GetChildrenCount(depObj); i++)
{
DependencyObject child = VisualTreeHelper.GetChild(depObj, i);
if (child != null && child is T)
{
yield return (T)child;
}
foreach (T childOfChild in FindVisualChildren(child))
{
yield return childOfChild;
}
}
}
}
}
}