using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Reflection;
using System.Runtime.InteropServices.ComTypes;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Shapes;
using XHandler.Class;
namespace XHandler.Controls
{
///
/// 板位类型耗材
///
public class ShelfLabware : Grid
{
//private int nBaseWidth = 16;
//private int nBaseHeight = 9;
///
/// 耗材的宽(真实比例换算之后的像素宽度)
///
public double ShelfWidth
{
get { return (double)GetValue(ShelfWidthProperty); }
set { SetValue(ShelfWidthProperty, value); }
}
public static readonly DependencyProperty ShelfWidthProperty =
DependencyProperty.Register("ShelfWidth", typeof(double), typeof(ShelfLabware), new PropertyMetadata(Double.NaN));
///
/// 耗材的长(真实比例换算之后的像素宽度)
///
public double ShelfLength
{
get { return (double)GetValue(ShelfLengthProperty); }
set { SetValue(ShelfLengthProperty, value); }
}
public static readonly DependencyProperty ShelfLengthProperty =
DependencyProperty.Register("ShelfLength", typeof(double), typeof(ShelfLabware), new PropertyMetadata(Double.NaN));
///
/// 板位的名称
///
public string ShelfName
{
get { return (string)GetValue(ShelfNameProperty); }
set { SetValue(ShelfNameProperty, value); }
}
public static readonly DependencyProperty ShelfNameProperty =
DependencyProperty.Register("ShelfName", typeof(string), typeof(ShelfLabware), new PropertyMetadata("", OnLabelChanged));
///
/// 板位是垃圾桶
///
public bool ShelfTrash
{
get { return (bool)GetValue(ShelfTrashProperty); }
set { SetValue(ShelfTrashProperty, value); }
}
public static readonly DependencyProperty ShelfTrashProperty =
DependencyProperty.Register("ShelfTrash", typeof(bool), typeof(ShelfLabware), new PropertyMetadata(false, OnShelfTrashChanged));
///
/// 板位的数据库Id
///
public string ShelfDBId
{
get { return (string)GetValue(ShelfDBIdProperty); }
set { SetValue(ShelfDBIdProperty, value); }
}
public static readonly DependencyProperty ShelfDBIdProperty =
DependencyProperty.Register("ShelfDBId", typeof(string), typeof(ShelfLabware), new PropertyMetadata(""));
public bool IsSelected
{
get { return (bool)GetValue(IsSelectedProperty); }
set { SetValue(IsSelectedProperty, value); }
}
// 依赖属性
public static readonly DependencyProperty IsSelectedProperty =
DependencyProperty.Register(
"IsSelected", typeof(bool), typeof(ShelfLabware),
new PropertyMetadata(false, OnIsSelectedChanged));
protected override void OnMouseLeftButtonUp(MouseButtonEventArgs e)
{
base.OnMouseLeftButtonUp(e);
// 处理左键抬起的逻辑
}
private static void OnIsSelectedChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
ShelfLabware control = (ShelfLabware)d;
if (control != null)
{
// 当IsSelected属性改变时,这里可以添加更多的逻辑处理
if ((bool)e.NewValue)
{
// 设置控件被选中时的样式
//control.Background = Brushes.Blue;
RectangleGeometry rectangleGeometry = new RectangleGeometry(new Rect(0, 0, 70, 70), 0, 0);
GeometryDrawing MaskDrawing = new GeometryDrawing(Brushes.White, null, rectangleGeometry);
DrawingBrush drawingBrush = new DrawingBrush(MaskDrawing);
control.OpacityMask = drawingBrush;
drawingBrush.Stretch = Stretch.Fill;
control.OpacityMask.Opacity = 0.5d;
//Rectangle rtl = new Rectangle();
//rtl.StrokeDashArray = new DoubleCollection { 4,2};
//rtl.Stroke = Brushes.Gray;
//rtl.StrokeThickness = 1d;
//GeometryDrawing MaskDrawing = new GeometryDrawing(Brushes.White, null, rtl.Geo);
//DrawingBrush drawingBrush = new DrawingBrush(MaskDrawing);
//control.OpacityMask = drawingBrush;
//control.OpacityMask.Opacity = 0.5d;
}
else
{
// 设置控件未被选中时的样式
//control.Background = Brushes.Transparent;
control.OpacityMask.Opacity = 1d;
}
}
}
public static void OnLabelChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
ShelfLabware control = (ShelfLabware)d;
if (control != null)
{
string label = e.NewValue as string;
control.SetLabel(label);
}
}
#region 改变为垃圾桶标志
public static void OnShelfTrashChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
ShelfLabware control = (ShelfLabware)d;
if (control != null)
{
bool isTrash = Convert.ToBoolean(e.NewValue);
if (isTrash)
{
if (borderTrash != null)
{
control.Children.Remove(borderTrash);
}
control.DrawTrash();
}
else
{
if (borderTrash != null)
{
control.Children.Remove(borderTrash);
}
}
}
}
#endregion
static Border borderTrash;
private void DrawTrash()
{
TextBlock tb = new TextBlock();
tb.Text = Properties.Resources.strTrash;
tb.FontSize = 10;
tb.FontWeight= FontWeights.Bold;
tb.Foreground = Brushes.Black;
tb.VerticalAlignment = VerticalAlignment.Center;
tb.HorizontalAlignment = HorizontalAlignment.Left;
borderTrash = new Border()
{
Name = "Trash",
Background = Brushes.DarkGray,
CornerRadius = new CornerRadius(5),
Child = tb,
};
//borderTrash.Loaded += BorderTrash_Loaded;
this.Children.Add(borderTrash);
Panel.SetZIndex(borderTrash, 4);
Grid.SetColumn(borderTrash, 0);
Grid.SetRow(borderTrash, 0);
}
private void BorderTrash_Loaded(object sender, RoutedEventArgs e)
{
Border tb = sender as Border;
if (tb != null)
{
tb.VerticalAlignment = VerticalAlignment.Center;
tb.HorizontalAlignment = HorizontalAlignment.Left;
}
}
public void SetLabel(string text)
{
if (borderLabel == null)
return;
TextBlock tb = Utilities.FindVisualChild(borderLabel);
if (tb != null)
{
tb.Text = text;
}
if (string.IsNullOrEmpty(text))
borderLabel.Visibility = Visibility.Collapsed;
else
{
borderLabel.Visibility = Visibility.Visible;
((ToolTip)(tb.ToolTip)).Content = new TextBlock()
{
Text = ShelfName,
};
}
}
public ShelfLabware()
{
this.SnapsToDevicePixels = true;
InitializeComponent();
}
void InitializeComponent()
{
tooltip = new ToolTip();
tooltip.Placement = PlacementMode.Right;
tooltip.PlacementRectangle = new Rect(0, 0, 0, 0);
tooltip.HorizontalOffset = 30;
tooltip.VerticalOffset = -20;
tooltipLabware = new ToolTip();
tooltipLabware.Placement = PlacementMode.Right;
tooltipLabware.PlacementRectangle = new Rect(0, 0, 0, 0);
tooltipLabware.HorizontalOffset = 30;
tooltipLabware.VerticalOffset = -20;
this.SizeChanged += ControlLabware_SizeChanged;
}
private void ControlLabware_SizeChanged(object sender, SizeChangedEventArgs e)
{
if (this.ActualWidth == double.NaN || this.ActualWidth <= 0)
return;
this.Children.Clear();
DrawContent();
AddLabel();
Width = ShelfLength;
Height = ShelfWidth;
}
private void DrawContent()
{
Path path = new Path()
{
Stroke = Brushes.Gray,//FindResource("blueBrush") as SolidColorBrush,
StrokeThickness = 1,
Fill = Brushes.DarkGray//FindResource("lightBlueBrush") as SolidColorBrush,
};
string strData = GetPathData();
path.Data = Geometry.Parse(strData);
this.Children.Add(path);
}
private string GetPathData()
{
string ret = "";
double W = ShelfLength;
double H = ShelfWidth;
ret = string.Format("M0,0 L{0},0 L{1},{2} L0,{3}Z", W, W, H, H);
return ret;
}
private Border borderLabel;
private ToolTip tooltip;//鼠标悬停至标签提示文本
private ToolTip tooltipLabware;//鼠标悬停至耗材提示文本
private void AddLabel()
{
if(string.IsNullOrEmpty(ShelfName))
{
return;
}
tooltip.Content = new TextBlock()
{
Text = ShelfName,
};
TextBlock tb = new TextBlock();
tb.Text = ShelfName;
tb.Margin = new Thickness(2);
tb.Foreground = Brushes.White;
tb.TextWrapping = TextWrapping.Wrap;
tb.ToolTip = tooltip;
tooltipLabware.Content = new TextBlock()
{
Text = ShelfName,
};
this.ToolTip = tooltipLabware;
borderLabel = new Border()
{
Name = "borderLabel",
VerticalAlignment = VerticalAlignment.Top,
HorizontalAlignment = HorizontalAlignment.Left,
Background = new SolidColorBrush(Color.FromArgb(180, 34, 106, 255)),
Width = this.ShelfLength,
Height = 20,
Child = tb,
};
if (string.IsNullOrEmpty(ShelfName))
borderLabel.Visibility = Visibility.Collapsed;
else
borderLabel.Visibility = Visibility.Visible;
this.Children.Add(borderLabel);
Panel.SetZIndex(borderLabel, 5);
Grid.SetColumn(borderLabel, 0);
//Grid.SetColumnSpan(borderLabel, 2);
Grid.SetRow(borderLabel, 0);
//Grid.SetRowSpan(borderLabel, 2);
}
}
}