using BPA.Models;
using BPA.Helper;
using BPASmartClient.MorkF.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.Imaging;
using System.Windows.Shapes;
namespace BPASmartClient.MorkF.View
{
///
/// LocalMenu.xaml 的交互逻辑
///
public partial class LocalMenu : Window
{
double X, Y;
Border ultUE;
Popup mypopup;
ListBoxItem OldItem;
ListBoxItem new_Item;
bool isDown = false;
int lao_index;
int new_index;
///
/// 当前拖拽子控件Item
///
private ListBoxItem ChildListBoxItem;
public LocalMenu()
{
InitializeComponent();
ActionManage.GetInstance.CancelRegister("LocalMenuClose");
ActionManage.GetInstance.Register(new Action(() =>
{
this.Hide();
}), "LocalMenuClose");
}
private void Button_Click(object sender, RoutedEventArgs e)
{
this.Hide();
}
private int GetCurrentIndex(GetPositionDelegate getPosition)
{
int index = -1;
for (int i = 0; i < listview1.Items.Count; ++i)
{
ListBoxItem 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);
ListBoxItem GetListViewItem(int index)
{
if (listview1.ItemContainerGenerator.Status != GeneratorStatus.ContainersGenerated)
return null;
return listview1.ItemContainerGenerator.ContainerFromIndex(index) as ListBoxItem;
}
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 (sender is ListBox listview && e.LeftButton == MouseButtonState.Pressed && listview.SelectedItem != null)
{
var pos = e.GetPosition(listview1);
HitTestResult result = VisualTreeHelper.HitTest(listview1, pos);
if (result == null)
{
return;
}
var listBoxItem = Utils.FindVisualParent(result.VisualHit);
if (listBoxItem == null || listBoxItem.Content != listview1.SelectedItem)
{
return;
}
DataObject dataObj = new DataObject(listBoxItem.Content as PotActions);
DragDrop.DoDragDrop(listview1, dataObj, DragDropEffects.Move);
ActionManage.GetInstance.Send("SortFryTime");
}
}
private void listview1_Drop(object sender, DragEventArgs e)
{
var pos = e.GetPosition(listview1);
var result = VisualTreeHelper.HitTest(listview1, pos);
if (result == null)
{
return;
}
//查找元数据
var sourcePerson = e.Data.GetData(typeof(PotActions)) as PotActions;
if (sourcePerson == null)
{
return;
}
//查找目标数据
var listBoxItem = Utils.FindVisualParent(result.VisualHit);
if (listBoxItem == null)
{
return;
}
var targetPerson = listBoxItem.Content as PotActions;
if (ReferenceEquals(targetPerson, sourcePerson))
{
return;
}
int i = listview1.Items.IndexOf(sourcePerson);
int j = listview1.Items.IndexOf(targetPerson);
ActionManage.GetInstance.Send(new int[] { i, j }, "PotActionStep");
//LocalMenuViewModel.PotActionStep.RemoveAt(listview1.Items.IndexOf(sourcePerson));
//LocalMenuViewModel.PotActionStep.Insert(listview1.Items.IndexOf(targetPerson)+1, sourcePerson);
//targetPerson.FryTime = listview1.Items.IndexOf(targetPerson) -1;
//LocalMenuViewModel.PotActionStep.Move(listview1.Items.IndexOf(sourcePerson), listview1.Items.IndexOf(targetPerson));
}
private void listview1_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
////鼠标未按下返回
//if (!isDown) return;
//isDown = false;
//var pos = e.GetPosition(listview1);
//HitTestResult result = VisualTreeHelper.HitTest(listview1, pos);
//if (result == null) return;
//new_Item = Utils.FindVisualParent(result.VisualHit);
//if (mypopup != null)
//{
// mypopup.IsOpen = false;
// mypopup = null;
//}
//MoveListBoxStyle(e, false);
}
private void Window_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
this.DragMove();
}
private void Materials_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
string str = Materials.SelectedValue?.ToString();
if (str != string.Empty)
{
ActionManage.GetInstance.Send("LoacMenuSelectMaterial", str);
}
}
///
/// 移动效果
///
///
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;
// item.Background = Brushes.AliceBlue;
// lao_index = LocalMenuViewModel.PotActionStep.IndexOf(item.Content as PotActions);
// new_index = LocalMenuViewModel.PotActionStep.IndexOf(ChildListBoxItem.Content as PotActions);
// //LocalMenuViewModel.PotActionStep.Move(lao_index, new_index);
// }
// else //鼠标没在哪一行,则保持原状
// {
// item.Opacity = 1;
// }
// }
// else
// {
// item.Visibility = Visibility.Hidden;
// }
//}
}
else//为假 恢复所有行透明度和显示状态
{
//LocalMenuViewModel.PotActionStep.Move(lao_index, new_index);
//移动到某行减轻某行 暗黑
foreach (ListBoxItem item in Utils.FindVisualChildren(listview1))
{
item.Background = Brushes.Transparent;
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;
}
}
}
}
}
}