using System;
|
using System.Collections.Generic;
|
using System.ComponentModel;
|
using System.Diagnostics;
|
using System.Linq;
|
using System.Windows;
|
using System.Windows.Controls;
|
using System.Windows.Input;
|
using System.Windows.Media;
|
using XHandler.Class;
|
|
namespace XHandler.Controls
|
{
|
/// <summary>
|
/// WellPlateEx.xaml 的交互逻辑
|
/// </summary>
|
public partial class WellPlateEx : UserControl
|
{
|
#region custom event
|
/// <summary>
|
/// 选中列事件,参数为选中的列数
|
/// </summary>
|
public static readonly RoutedEvent SelectedRoutedEvent =
|
EventManager.RegisterRoutedEvent("SelectedEvent", RoutingStrategy.Bubble, typeof(CustomEvent.CustomRoutedEventHandler<string>), typeof(WellPlateEx));
|
|
[Description("SelectedEvent")]
|
public event CustomEvent.CustomRoutedEventHandler<string> SelectedEvent
|
{
|
add
|
{
|
this.AddHandler(SelectedRoutedEvent, value);
|
}
|
remove
|
{
|
this.RemoveHandler(SelectedRoutedEvent, value);
|
}
|
}
|
|
private void RaiseSelectEvent(string str)
|
{
|
CustomRoutedEventArgs<string> arg = new CustomRoutedEventArgs<string>(SelectedRoutedEvent, str);
|
this.RaiseEvent(arg);
|
}
|
#endregion
|
|
private List<string> groupList = new List<string>();
|
/// <summary>
|
/// 行数
|
/// </summary>
|
public static readonly DependencyProperty RowsProperty = DependencyProperty.Register("Rows", typeof(int), typeof(WellPlateEx),
|
new FrameworkPropertyMetadata((int)8));
|
|
public int Rows
|
{
|
get { return (int)GetValue(RowsProperty); }
|
set { SetValue(RowsProperty, value); }
|
}
|
|
/// <summary>
|
/// 列数
|
/// </summary>
|
public static readonly DependencyProperty ColumnsProperty = DependencyProperty.Register("Columns", typeof(int), typeof(WellPlateEx),
|
new FrameworkPropertyMetadata((int)12, OnCountChanged));
|
|
public int Columns
|
{
|
get { return (int)GetValue(ColumnsProperty); }
|
set { SetValue(ColumnsProperty, value); }
|
}
|
|
public static void OnCountChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
|
{
|
WellPlateEx plate = (WellPlateEx)d;
|
Debug.WriteLine("rows=" + plate.Rows.ToString() + ", columns=" + plate.Columns.ToString());
|
|
plate.CreateWells();
|
}
|
|
public static readonly DependencyProperty SelectionProperty = DependencyProperty.Register("Selection", typeof(string), typeof(WellPlateEx),
|
new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.AffectsRender, new PropertyChangedCallback(OnSelectionChanged), null));
|
/// <summary>
|
/// 选择内容
|
/// </summary>
|
public string Selection
|
{
|
get { return (string)GetValue(SelectionProperty); }
|
set { SetValue(SelectionProperty, value); }
|
}
|
|
private static void OnSelectionChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
|
{
|
WellPlateEx plate = sender as WellPlateEx;
|
|
string str = (string)e.NewValue;
|
plate.SetSelection(str);
|
}
|
|
private List<string> list= new List<string>();
|
private Point startPoint;
|
|
public WellPlateEx()
|
{
|
InitializeComponent();
|
}
|
|
private void idGrid_PreviewMouseMove(object sender, MouseEventArgs e)
|
{
|
if (e.LeftButton == MouseButtonState.Pressed)
|
{
|
Point curPos = e.GetPosition(idGrid);
|
if ((Math.Abs(curPos.X - startPoint.X) > SystemParameters.MinimumHorizontalDragDistance)
|
|| (Math.Abs(curPos.Y - startPoint.Y) > SystemParameters.MinimumVerticalDragDistance))
|
{
|
Rect rect = GetCorrectRect(startPoint, curPos);
|
List<int> listCol = new List<int>();
|
foreach (var child in idGrid.Children)
|
{
|
if (child is Border)
|
{
|
Border border = (Border)child;
|
GeneralTransform generalTransform1 = border.TransformToAncestor(idGrid);
|
Point currentPoint = generalTransform1.Transform(new Point(0, 0));
|
Rect childRect = new Rect(currentPoint.X, currentPoint.Y, border.ActualWidth, border.ActualHeight);
|
if (childRect.IntersectsWith(rect))
|
{
|
border.Background = FindResource("blueBrush") as SolidColorBrush;
|
if (!list.Contains(border.Name))
|
list.Add(border.Name);
|
|
}
|
else
|
{
|
TextBlock tb = border.Child as TextBlock;
|
if (string.IsNullOrEmpty(tb.Text))
|
{
|
border.Background = Brushes.White;
|
if (list.Contains(border.Name))
|
list.Remove(border.Name);
|
}
|
}
|
}
|
}
|
}
|
}
|
}
|
private string ChangePosToString(int row, int col)
|
{
|
string ret = "";
|
char ch = (char)('A' + row);
|
string strRow = ch.ToString();
|
ret = strRow + (col+1).ToString();
|
return ret;
|
}
|
private void UserControl_Loaded(object sender, RoutedEventArgs e)
|
{
|
this.MouseLeave += WellPlate_MouseLeave;
|
idGrid.MouseLeave += idGrid_MouseLeave;
|
|
CreateWells();
|
|
if(groupList.Count>0)
|
{
|
foreach(Border bd in idGrid.Children)
|
{
|
TextBlock tb = bd.Child as TextBlock;
|
int i = 1;
|
foreach (string wells in groupList)
|
{
|
string[] ary = wells.Split(',');
|
if(ary.Contains(bd.Name))
|
{
|
bd.Background = FindResource("blueBrush") as SolidColorBrush;
|
tb.Text = i.ToString();
|
}
|
i++;
|
}
|
}
|
}
|
}
|
|
private void WellPlate_MouseLeave(object sender, MouseEventArgs e)
|
{
|
}
|
|
private void idGrid_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
|
{
|
startPoint = e.GetPosition(idGrid);
|
|
list.Clear();
|
if (e.Source is Border)
|
{
|
Border element = (Border)e.Source;
|
if (element == null)
|
return;
|
|
element.Background = FindResource("blueBrush") as SolidColorBrush;
|
list.Add(element.Name);
|
}
|
|
}
|
|
private void idGrid_PreviewMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
|
{
|
|
string str = "";
|
foreach(Border rect in idGrid.Children)
|
{
|
if(list.Contains(rect.Name))
|
{
|
if (string.IsNullOrEmpty(str))
|
str = rect.Name;
|
else
|
str += "," + rect.Name;
|
}
|
}
|
|
Selection = str;
|
}
|
|
private void idGrid_MouseLeave(object sender, MouseEventArgs e)
|
{
|
if (e.LeftButton == MouseButtonState.Pressed)
|
{
|
string str = "";
|
foreach (Border rect in idGrid.Children)
|
{
|
if (list.Contains(rect.Name))
|
{
|
if (string.IsNullOrEmpty(str))
|
str = rect.Name;
|
else
|
str += "," + rect.Name;
|
}
|
}
|
|
Selection = str;
|
}
|
}
|
|
private void UpdatePlate()
|
{
|
if (idGrid.Children.Count == 0)
|
return;
|
int count = Rows * Columns;
|
for (int i = 0; i < count; i++)
|
{
|
Border border = idGrid.Children[i] as Border;
|
if (list.Contains(border.Name))
|
border.Background = FindResource("blueBrush") as SolidColorBrush;
|
else
|
{
|
TextBlock tb = border.Child as TextBlock;
|
if(string.IsNullOrEmpty(tb.Text))
|
border.Background = Brushes.White;
|
}
|
}
|
}
|
|
public string GetSettingWells()
|
{
|
string ret = "";
|
if (groupList.Count == 0)
|
return ret;
|
ret = string.Join(",", groupList);
|
return ret;
|
}
|
|
public void CreateWells()
|
{
|
topGrid.Children.Clear();
|
topGrid.ColumnDefinitions.Clear();
|
leftGrid.Children.Clear();
|
leftGrid.RowDefinitions.Clear();
|
idGrid.Children.Clear();
|
idGrid.RowDefinitions.Clear();
|
idGrid.ColumnDefinitions.Clear();
|
|
for (int column = 0; column < Columns; column++)
|
{
|
topGrid.ColumnDefinitions.Add(new ColumnDefinition());
|
TextBlock tb = new TextBlock()
|
{
|
Text = (column + 1).ToString(),
|
Style = FindResource("smallTextStyle1") as Style,
|
};
|
topGrid.Children.Add(tb);
|
Grid.SetColumn(tb, column);
|
idGrid.ColumnDefinitions.Add(new ColumnDefinition());
|
}
|
for (int row = 0; row < Rows; row++)
|
{
|
leftGrid.RowDefinitions.Add(new RowDefinition());
|
char ch = (char)('A' + row);
|
TextBlock tb = new TextBlock()
|
{
|
Text = ch.ToString(),
|
Style = FindResource("smallTextStyle2") as Style,
|
};
|
leftGrid.Children.Add(tb);
|
Grid.SetRow(tb, row);
|
idGrid.RowDefinitions.Add(new RowDefinition());
|
}
|
for (int column = 0; column < Columns; column++)
|
{
|
for (int row = 0; row < Rows; row++)
|
{
|
Border border = new Border();
|
border.Name = ChangePosToString(row, column);
|
if (Columns > 12)
|
border.Margin = new Thickness(2, 2, 2, 2);
|
else
|
border.Margin = new Thickness(4, 4, 4, 4);
|
border.Background = Brushes.White;
|
border.BorderBrush = Brushes.Gray;
|
border.BorderThickness = new Thickness(1,1,1,1);
|
|
TextBlock textBlock = new TextBlock();
|
textBlock.Foreground = Brushes.White;
|
textBlock.HorizontalAlignment = System.Windows.HorizontalAlignment.Center;
|
textBlock.VerticalAlignment = System.Windows.VerticalAlignment.Center;
|
|
border.Child = textBlock;
|
idGrid.Children.Add(border);
|
Grid.SetRow(border, row);
|
Grid.SetColumn(border, column);
|
}
|
}
|
}
|
|
public void SetSelection(string selection)
|
{
|
list.Clear();
|
if (selection == null)
|
return;
|
string[] ary = selection.Split(',');
|
foreach (string s in ary)
|
{
|
if(string.IsNullOrEmpty(s)) break;
|
|
list.Add(s);
|
}
|
|
UpdatePlate();
|
}
|
Rect GetCorrectRect(Point start, Point end)
|
{
|
if (end.X < start.X)
|
{
|
double tmp = start.X;
|
start.X = end.X;
|
end.X = tmp;
|
}
|
if (end.Y < start.Y)
|
{
|
double tmp = start.Y;
|
start.Y = end.Y;
|
end.Y = tmp;
|
}
|
return new Rect(start.X, start.Y, end.X - start.X, end.Y - start.Y);
|
}
|
|
public void AddGroup(string wells)
|
{
|
if(string.IsNullOrEmpty(wells))
|
return;
|
string[] ary = wells.Split(',');
|
List<string> wellList = new List<string>(ary);
|
int count = groupList.Count;
|
for(int i=0;i<count; i++)
|
{
|
string str = groupList[i];
|
string[] strArry = str.Split(',');
|
|
foreach(string well in strArry)
|
{
|
if (wellList.Contains(well))
|
wellList.Remove(well);
|
}
|
}
|
if (wellList.Count == 0)
|
return;
|
string newWells = string.Join(",", wellList);
|
groupList.Add(newWells);
|
int index = groupList.Count;
|
foreach (Border bd in idGrid.Children)
|
{
|
if(wellList.Contains(bd.Name))
|
{
|
bd.Background = FindResource("blueBrush") as SolidColorBrush;
|
TextBlock tb = bd.Child as TextBlock;
|
tb.Text = index.ToString();
|
}
|
}
|
}
|
|
public void ClearGroup()
|
{
|
groupList.Clear();
|
Selection = "";
|
foreach (Border bd in idGrid.Children)
|
{
|
TextBlock tb = bd.Child as TextBlock;
|
tb.Text = "";
|
bd.Background = Brushes.White;
|
}
|
}
|
|
}
|
}
|