1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099 |
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Linq;
- using System.Reflection;
- using System.Windows;
- using System.Windows.Controls;
- using System.Windows.Data;
- using System.Windows.Media;
- using Venus_Core;
- using Venus_Core.Attributes;
- namespace Aitex.UI.RecipeEditor
- {
- public class RecipeEditorControlViewModel : INotifyPropertyChanged
- {
- //当前选中的配方
- private Recipe m_currentRecipe;
- public Recipe CurrentRecipe
- {
- get { return m_currentRecipe; }
- set { m_currentRecipe = value; InvokePropertyChanged("CurrentRecipe"); }
- }
-
- public StackPanel GridStackPanel { get; set; } //step容器
- List<SolidColorBrush> solidColorBrushes = new List<SolidColorBrush>();//step模块颜色
- public WrapPanel HeadWrapPanel { get; set; } //head容器
- Grid currentGrid;
-
- public RecipeEditorControlViewModel()
- {
- solidColorBrushes.Add(new SolidColorBrush(Colors.Coral));
- solidColorBrushes.Add(new SolidColorBrush(Colors.Cyan));
- solidColorBrushes.Add(new SolidColorBrush(Colors.Honeydew));
- }
- public void OnAddStep()
- {
- if (CurrentRecipe != null)
- {
- RecipeStep recipeStep = new RecipeStep();
- recipeStep.LstUnit.Add(new PressureByPressureModeUnit());
- recipeStep.LstUnit.Add(new TCPUnit());
- recipeStep.LstUnit.Add(new BiasUnit());
- recipeStep.LstUnit.Add(new GasControlUnit());
- recipeStep.LstUnit.Add(new ESCHVUnit());
- recipeStep.LstUnit.Add(new ProcessKitUnit());
- recipeStep.StepNo = currentGrid.ColumnDefinitions.Count;
- CurrentRecipe.Steps.Add(recipeStep);
- //var item = RecipeStepToGrid(recipeStep);
- //GridStackPanel.Children.Add(item.Item1);
- RecipeStepToGridColumn(recipeStep, currentGrid);
- }
- }
- public void OnDeleteStep()
- {
- if (CurrentRecipe != null&& CurrentRecipe.Steps.Count>0)
- {
- CurrentRecipe.Steps.RemoveAt(CurrentRecipe.Steps.Count - 1);
- LoadRecipe(CurrentRecipe);
- }
- }
- int index = 0;
- /// <summary>
- /// 选中recipe,显示界面
- /// </summary>
- /// <param name="recipe"></param>
- public void LoadRecipe(Recipe recipe)
- {
- GridStackPanel.Children.Clear();
- CurrentRecipe = null;
-
- GC.Collect(); // This should pick up the control removed at a previous MouseDown
- GC.WaitForPendingFinalizers(); // Doesn't help either
- GC.Collect();
- GC.WaitForPendingFinalizers(); // Doesn't help either
- CurrentRecipe = recipe;
- Grid grid = new Grid();
- grid.Margin = new Thickness(15);
- GridOptions.SetShowBorder(grid, true);
- GridOptions.SetLineBrush(grid, Brushes.Black);
- GridOptions.SetLineThickness(grid, 1);
- index = 0;
- recipe.Steps.ToList().ForEach(x =>
- {
- RecipeStepToGridColumn(x,grid);
- });
- GridStackPanel.Children.Add(grid);
- LoadHeadWrapPanel(recipe);
- currentGrid = grid;
- }
- /// <summary>
- /// 选中recipe,头部界面显示
- /// </summary>
- /// <param name="recipe"></param>
- private void LoadHeadWrapPanel(Recipe recipe)
- {
- HeadWrapPanel.Children.Clear();
- Type type = recipe.Header.GetType();
- foreach (var propertyInfo in type.GetProperties())
- {
- TextBlock textBlock = new TextBlock();
- textBlock.FontSize = 15;
- //textBlock.Width = 100;
- textBlock.Text = propertyInfo.Name + ":";
- textBlock.Margin = new Thickness(15, 0, 0, 0);
- textBlock.VerticalAlignment = VerticalAlignment.Center;
- HeadWrapPanel.Children.Add(textBlock);
- Binding binding = new Binding()
- {
- Source = recipe.Header, // 数据源
- Path = new PropertyPath(propertyInfo.Name), // 需绑定的数据源属性名
- Mode = BindingMode.TwoWay, // 绑定模式
- UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged //触发器
- };
- var propertyTypeName = propertyInfo.PropertyType.Name;
- var propertyInfoName=propertyInfo.Name;
- Control control = new Control();
- switch (propertyTypeName)
- {
- case "Int32":
- case "String":
- control = new TextBox();
-
- control.Margin = new Thickness(1, 0, 0, 0);
- control.BorderThickness = new Thickness(0,0,0,1);
- //control.BorderBrush = Brushes.Black;
- control.FontSize = 15;
- control.Foreground = Brushes.Green;
- control.Background = Brushes.Transparent;
- control.VerticalAlignment = VerticalAlignment.Center;
- control.MinWidth = 15;
- //control.Width = 200;
- control.SetBinding(TextBox.TextProperty, binding);
- //if (propertyInfoName == "Name" || propertyInfoName == "CreateTime" || propertyInfoName == "LastModifiedBy")
- //{
- // (control as TextBox).IsReadOnly = true;
- //}
- object[] objAttrs = propertyInfo.GetCustomAttributes(typeof(IsOnlyReadAttribute), true);
- if (objAttrs.Length > 0)
- {
- (control as TextBox).IsReadOnly = true;
- }
- break;
- case "Boolean":
- control = new CheckBox();
- control.SetBinding(CheckBox.IsCheckedProperty, binding);
- break;
- default:
- control = new ComboBox();
- control.Height = 23;
- control.SetBinding(ComboBox.SelectedItemProperty, binding);
- ItemsControlHelper.SetEnumValuesToItemsSource(control, true);
- break;
- }
- HeadWrapPanel.Children.Add(control);
- }
- }
- private void RecipeStepToGridColumn(RecipeStep recipeStep, Grid grid)
- {
- Type recipeType = recipeStep.GetType();
- int i = 0;
- if (grid.ColumnDefinitions.Count == 0)
- {
- ColumnDefinition col1 = new ColumnDefinition();//Key
- grid.ColumnDefinitions.Add(col1);
- }
- ColumnDefinition col2 = new ColumnDefinition();//Value
- grid.ColumnDefinitions.Add(col2);
-
- foreach (PropertyInfo propertyInfo in recipeType.GetProperties())
- {
- string propertyInfoName = propertyInfo.Name;
- string propertyTypeName = propertyInfo.PropertyType.Name;
- if (propertyInfoName != "LstUnit")
- {
- if (index == 0)
- {
- RowDefinition row1 = new RowDefinition();
- grid.RowDefinitions.Add(row1);
- }
-
- Binding binding = new Binding()
- {
- Source = recipeStep, // 数据源
- Path = new PropertyPath(propertyInfoName), // 需绑定的数据源属性名
- Mode = BindingMode.TwoWay, // 绑定模式
- UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged //触发器
- };
- switch (propertyTypeName)
- {
- case "Int32":
- case "String":
- TextBox textBox = new TextBox();
- textBox.BorderThickness = new Thickness(0);
- //textBox.Background = new SolidColorBrush(Colors.BurlyWood);
- textBox.SetBinding(TextBox.TextProperty, binding);
- grid.Children.Add(textBox);
- Grid.SetRow(textBox, i);
- Grid.SetColumn(textBox, grid.ColumnDefinitions.Count - 1);
- object[] objAttrs = propertyInfo.GetCustomAttributes(typeof(IsOnlyReadAttribute), true);
- if (objAttrs.Length > 0)
- {
- textBox.IsReadOnly = true;
- }
- break;
- case "Boolean":
- CheckBox checkBox = new CheckBox();
- //checkBox.Background = new SolidColorBrush(Colors.BurlyWood);
- //checkBox.Margin = new Thickness(5);
- checkBox.SetBinding(CheckBox.IsCheckedProperty, binding);
- grid.Children.Add(checkBox);
- Grid.SetRow(checkBox, i);
- Grid.SetColumn(checkBox, grid.ColumnDefinitions.Count - 1);
- break;
- default:
- ComboBox comboBox = new ComboBox();
- comboBox.BorderThickness = new Thickness(0);
- comboBox.Style = new Style();
-
- //comboBox.Background=new SolidColorBrush(Colors.BurlyWood);
- comboBox.SetBinding(ComboBox.SelectedItemProperty, binding);
- ItemsControlHelper.SetEnumValuesToItemsSource(comboBox, true);
- grid.Children.Add(comboBox);
- Grid.SetRow(comboBox, i);
- Grid.SetColumn(comboBox, grid.ColumnDefinitions.Count - 1);
- //if (propertyInfo.Name == "PressureUnitMode")
- //{
- // comboBox.SelectionChanged += ComboBox_SelectionChanged;
- //}
- break;
- }
- if (grid.ColumnDefinitions.Count == 2)
- {
- TextBlock textBlock = new TextBlock();
- textBlock.Text = propertyInfoName;
- grid.Children.Add(textBlock);
- textBlock.Background = new SolidColorBrush(Colors.BurlyWood);
- Grid.SetRow(textBlock, i);
- Grid.SetColumn(textBlock, 0);
- }
- i++;
- }
- }
- //ContextMenu contextmenu = new ContextMenu();
- //contextmenu.Tag = grid;
- //grid.ContextMenu = contextmenu;
- //MenuItem menuItemDelete = new MenuItem();
- //menuItemDelete.Header = "删除";
- //menuItemDelete.Click += MenuItemDelete_Click;
- //MenuItem menuItemInsert = new MenuItem();
- //menuItemInsert.Header = "左插入";
- //menuItemInsert.Click += MenuItemInsert_Click;
- //contextmenu.Items.Add(menuItemDelete);
- //contextmenu.Items.Add(menuItemInsert);
- List<int> ints = new List<int>();
- ints.Add(recipeType.GetProperties().Length);
- int k = 0;
- recipeStep.LstUnit.ToList().ForEach(x =>
- {
- Type unitType = x.GetType();
- foreach (PropertyInfo propertyInfo in unitType.GetProperties())
- {
- Binding binding = new Binding()
- {
- Source = recipeStep.LstUnit[k], // 数据源
- //Path = new PropertyPath($"LstUnit[{k}]." + propertyInfo.Name), // 需绑定的数据源属性名
- Path = new PropertyPath(propertyInfo.Name), // 需绑定的数据源属性名
- Mode = BindingMode.TwoWay, // 绑定模式
- UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged //触发器
- };
- if (index == 0)
- {
- RowDefinition row1 = new RowDefinition();
- grid.RowDefinitions.Add(row1);
- }
- var item = propertyInfo.PropertyType.Name;
- if (propertyInfo.Name == "UnitName")
- {
- TextBox textBlock1 = new TextBox();
- //textBlock1.SetBinding(TextBlock.TextProperty, binding);
- textBlock1.Text = propertyInfo.GetValue(x).ToString();
- textBlock1.BorderThickness = new Thickness(0);
- textBlock1.Foreground = Brushes.Red;
- textBlock1.IsReadOnly = true;
- //textBlock1.HorizontalAlignment = HorizontalAlignment.Center;
- grid.Children.Add(textBlock1);
- Grid.SetRow(textBlock1, i);
- Grid.SetColumn(textBlock1, grid.ColumnDefinitions.Count - 1);
- }
- else
- {
- switch (item)
- {
- case "Int32":
- case "String":
- TextBox textBox = new TextBox();
- textBox.BorderBrush = Brushes.Transparent;
- textBox.BorderThickness = new Thickness(0);
- textBox.SetBinding(TextBox.TextProperty, binding);
- grid.Children.Add(textBox);
- Grid.SetRow(textBox, i);
- Grid.SetColumn(textBox, grid.ColumnDefinitions.Count - 1);
- object[] objAttrs = propertyInfo.GetCustomAttributes(typeof(IsOnlyReadAttribute), true);
- if (objAttrs.Length > 0)
- {
- textBox.IsReadOnly = true;
- }
- break;
- case "Boolean":
- CheckBox checkBox = new CheckBox();
- checkBox.Margin = new Thickness(1);
- checkBox.SetBinding(CheckBox.IsCheckedProperty, binding);
- grid.Children.Add(checkBox);
- Grid.SetRow(checkBox, i);
- Grid.SetColumn(checkBox, grid.ColumnDefinitions.Count - 1);
- break;
- default:
- ComboBox comboBox = new ComboBox();
- comboBox.Style = new Style();
- comboBox.BorderThickness = new Thickness(0);
- comboBox.Background = Brushes.White;
- string path = propertyInfo.Name;
- comboBox.SetBinding(ComboBox.SelectedItemProperty, binding);
- ItemsControlHelper.SetEnumValuesToItemsSource(comboBox, true);
- grid.Children.Add(comboBox);
- Grid.SetRow(comboBox, i);
- Grid.SetColumn(comboBox, grid.ColumnDefinitions.Count - 1);
- break;
- }
- }
- if (grid.ColumnDefinitions.Count == 2)
- {
- TextBlock textBlock = new TextBlock();
- textBlock.Text = propertyInfo.Name;
- textBlock.Background = solidColorBrushes[k % 3];
- grid.Children.Add(textBlock);
- Grid.SetRow(textBlock, i);
- Grid.SetColumn(textBlock, 0);
- }
- i++;
- }
- ints.Add(unitType.GetProperties().Length);
- k++;
- });
- index++;
- //return new ValueTuple<Grid, List<int>>(grid, ints);
- }
- /// <summary>
- /// step转换为Grid
- /// </summary>
- /// <param name="recipeStep"></param>
- /// <returns></returns>
- private ValueTuple<Grid, List<int>> RecipeStepToGrid(RecipeStep recipeStep)
- {
- Grid grid = new Grid();
- ContextMenu contextmenu = new ContextMenu();
- contextmenu.Tag = grid;
- grid.ContextMenu= contextmenu;
- MenuItem menuItemDelete = new MenuItem();
- menuItemDelete.Header = "删除";
- menuItemDelete.Click += MenuItemDelete_Click;
- MenuItem menuItemInsert = new MenuItem();
- menuItemInsert.Header = "左插入";
- menuItemInsert.Click += MenuItemInsert_Click;
- contextmenu.Items.Add(menuItemDelete);
- contextmenu.Items.Add(menuItemInsert);
- List<int> ints = new List<int>();
- ColumnDefinition col1 = new ColumnDefinition();//Key
- ColumnDefinition col2 = new ColumnDefinition();//Value
- grid.ColumnDefinitions.Add(col1);
- grid.ColumnDefinitions.Add(col2);
- GridOptions.SetShowBorder(grid, true);
- GridOptions.SetLineBrush(grid, Brushes.White);
- GridOptions.SetLineThickness(grid, 1);
- grid.Margin = new Thickness(15, 5, 0, 0);
- Type recipeType = recipeStep.GetType();
- int i = 0;
- foreach (PropertyInfo propertyInfo in recipeType.GetProperties())
- {
- string propertyInfoName = propertyInfo.Name;
- string propertyTypeName = propertyInfo.PropertyType.Name;
- if (propertyInfoName != "LstUnit")
- {
- RowDefinition row1 = new RowDefinition();
- grid.RowDefinitions.Add(row1);
- Binding binding = new Binding()
- {
- Source = recipeStep, // 数据源
- Path = new PropertyPath(propertyInfoName), // 需绑定的数据源属性名
- Mode = BindingMode.TwoWay, // 绑定模式
- UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged //触发器
- };
- switch (propertyTypeName)
- {
- case "Int32":
- case "String":
- TextBox textBox = new TextBox();
- textBox.SetBinding(TextBox.TextProperty, binding);
- grid.Children.Add(textBox);
- Grid.SetRow(textBox, i);
- Grid.SetColumn(textBox, 1);
- object[] objAttrs = propertyInfo.GetCustomAttributes(typeof(IsOnlyReadAttribute), true);
- if (objAttrs.Length > 0)
- {
- textBox.IsReadOnly = true;
- }
- break;
- case "Boolean":
- CheckBox checkBox = new CheckBox();
- checkBox.SetBinding(CheckBox.IsCheckedProperty, binding);
- grid.Children.Add(checkBox);
- Grid.SetRow(checkBox, i);
- Grid.SetColumn(checkBox, 1);
- break;
- default:
- ComboBox comboBox = new ComboBox();
- comboBox.SetBinding(ComboBox.SelectedItemProperty, binding);
- ItemsControlHelper.SetEnumValuesToItemsSource(comboBox, true);
- grid.Children.Add(comboBox);
- Grid.SetRow(comboBox, i);
- Grid.SetColumn(comboBox, 1);
- if (propertyInfo.Name == "PressureUnitMode")
- {
- comboBox.SelectionChanged += ComboBox_SelectionChanged;
- }
- break;
- }
- TextBlock textBlock = new TextBlock();
- textBlock.Text = propertyInfoName;
- grid.Children.Add(textBlock);
- Grid.SetRow(textBlock, i);
- Grid.SetColumn(textBlock, 0);
- i++;
- }
- }
- ints.Add(recipeType.GetProperties().Length);
- int k = 0;
- recipeStep.LstUnit.ToList().ForEach(x =>
- {
- Type unitType = x.GetType();
- foreach (PropertyInfo propertyInfo in unitType.GetProperties())
- {
- Binding binding = new Binding()
- {
- Source = recipeStep.LstUnit[k], // 数据源
- //Path = new PropertyPath($"LstUnit[{k}]." + propertyInfo.Name), // 需绑定的数据源属性名
- Path = new PropertyPath(propertyInfo.Name), // 需绑定的数据源属性名
- Mode = BindingMode.TwoWay, // 绑定模式
- UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged //触发器
- };
- RowDefinition row1 = new RowDefinition();
- grid.RowDefinitions.Add(row1);
- var item = propertyInfo.PropertyType.Name;
- if (propertyInfo.Name == "UnitName")
- {
- TextBlock textBlock1 = new TextBlock();
- //textBlock1.SetBinding(TextBlock.TextProperty, binding);
- textBlock1.Text = propertyInfo.GetValue(x).ToString();
- textBlock1.Foreground = Brushes.Red;
- //textBlock1.HorizontalAlignment = HorizontalAlignment.Center;
- grid.Children.Add(textBlock1);
- Grid.SetRow(textBlock1, i);
- Grid.SetColumn(textBlock1, 1);
- }
- else
- {
- switch (item)
- {
- case "Int32":
- case "String":
- TextBox textBox = new TextBox();
- textBox.BorderBrush = Brushes.Transparent;
- textBox.SetBinding(TextBox.TextProperty, binding);
- grid.Children.Add(textBox);
- Grid.SetRow(textBox, i);
- Grid.SetColumn(textBox, 1);
- object[] objAttrs = propertyInfo.GetCustomAttributes(typeof(IsOnlyReadAttribute), true);
- if (objAttrs.Length > 0)
- {
- textBox.IsReadOnly = true;
- }
- break;
- case "Boolean":
- CheckBox checkBox = new CheckBox();
- checkBox.SetBinding(CheckBox.IsCheckedProperty, binding);
- grid.Children.Add(checkBox);
- Grid.SetRow(checkBox, i);
- Grid.SetColumn(checkBox, 1);
- break;
- default:
- ComboBox comboBox = new ComboBox();
- comboBox.BorderBrush = Brushes.Transparent;
- //comboBox.Background = Brushes.White;
- string path = propertyInfo.Name;
- comboBox.SetBinding(ComboBox.SelectedItemProperty, binding);
- ItemsControlHelper.SetEnumValuesToItemsSource(comboBox, true);
- grid.Children.Add(comboBox);
- Grid.SetRow(comboBox, i);
- Grid.SetColumn(comboBox, 1);
- break;
- }
- }
- TextBlock textBlock = new TextBlock();
- textBlock.Text = propertyInfo.Name;
- grid.Children.Add(textBlock);
- Grid.SetRow(textBlock, i);
- Grid.SetColumn(textBlock, 0);
- i++;
- }
- ints.Add(unitType.GetProperties().Length);
- k++;
- });
- return new ValueTuple<Grid, List<int>>(grid, ints);
- }
- private void MenuItemInsert_Click(object sender, RoutedEventArgs e)
- {
- MenuItem item = sender as MenuItem;
- ContextMenu contextMenu = item.Parent as ContextMenu;
- Grid grid = contextMenu.Tag as Grid;
- int index = GridStackPanel.Children.IndexOf(grid);
- RecipeStep recipeStep = new RecipeStep();
- recipeStep.LstUnit.Add(new PressureByPressureModeUnit());
- recipeStep.LstUnit.Add(new TCPUnit());
- recipeStep.LstUnit.Add(new BiasUnit());
- recipeStep.LstUnit.Add(new GasControlUnit());
- recipeStep.LstUnit.Add(new ESCHVUnit());
- recipeStep.LstUnit.Add(new ProcessKitUnit());
-
-
- CurrentRecipe.Steps.Insert(index , recipeStep);
- var item2 = RecipeStepToGrid(recipeStep);
- GridStackPanel.Children.Insert(index, item2.Item1);
- for (int i = 0; i < CurrentRecipe.Steps.Count; i++)
- {
- CurrentRecipe.Steps[i].StepNo = i+1;
- }
-
- }
- private void MenuItemDelete_Click(object sender, RoutedEventArgs e)
- {
- MenuItem item = sender as MenuItem;
- ContextMenu contextMenu = item.Parent as ContextMenu;
- Grid grid = contextMenu.Tag as Grid;
- int index = GridStackPanel.Children.IndexOf(grid);
- GridStackPanel.Children.Remove(grid);
- CurrentRecipe.Steps.RemoveAt(index);
- for (int i = 0; i < CurrentRecipe.Steps.Count; i++)
- {
- CurrentRecipe.Steps[i].StepNo = i + 1;
- }
- }
- /// <summary>
- /// 获取父控件
- /// </summary>
- /// <typeparam name="T"></typeparam>
- /// <param name="obj"></param>
- /// <param name="name"></param>
- /// <returns></returns>
- public T GetParentObject2<T>(DependencyObject obj, string name) where T : FrameworkElement
- {
- DependencyObject parent = VisualTreeHelper.GetParent(obj);
- while (parent != null)
- {
- if (parent is T && (((T)parent).Name == name || string.IsNullOrEmpty(name)))
- {
- return (T)parent;
- }
- parent = VisualTreeHelper.GetParent(parent);
- }
- return null;
- }
- private void ComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
- {
- if (e.RemovedItems.Count > 0)
- {
- ComboBox comboBox = sender as ComboBox;
- Grid grid = GetParentObject<Grid>(comboBox, "");
- int index= GridStackPanel.Children.IndexOf(grid);
- RecipeStep recipeStep = CurrentRecipe.Steps[index];
- recipeStep.LstUnit.RemoveAt(0);
- if (comboBox.SelectedIndex == 0)
- {
- recipeStep.LstUnit.Insert(0, new PressureByPressureModeUnit());
- }
- else
- {
- recipeStep.LstUnit.Insert(0, new PressureByValveModeUnit());
- }
- GridStackPanel.Children.Remove(grid);
- GridStackPanel.Children.Insert(index, RecipeStepToGrid(recipeStep).Item1);
- }
- }
- /// <summary>
- /// 获取父控件
- /// </summary>
- /// <typeparam name="T"></typeparam>
- /// <param name="obj"></param>
- /// <param name="name"></param>
- /// <returns></returns>
- public T GetParentObject<T>(DependencyObject obj, string name) where T : FrameworkElement
- {
- DependencyObject parent = VisualTreeHelper.GetParent(obj);
- while (parent != null)
- {
- if (parent is T && (((T)parent).Name == name || string.IsNullOrEmpty(name)))
- {
- return (T)parent;
- }
- parent = VisualTreeHelper.GetParent(parent);
- }
- return null;
- }
- private Tuple<DataGrid,List<int>> RecipeStepToDataGrid(RecipeStep recipeStep)
- {
- DataGrid dataGrid = new DataGrid();
- List<int> ints = new List<int>();
- DataTable dataTable = new DataTable();
- dataTable.Columns.Add("Key");
- dataTable.Columns.Add("Value");
- dataGrid.CanUserAddRows = false;
- dataGrid.AutoGenerateColumns = true;
- dataGrid.SelectionUnit = DataGridSelectionUnit.Cell;
- dataGrid.SetValue(System.Windows.Controls.VirtualizingStackPanel.IsVirtualizingProperty, false);
- Type recipeType = recipeStep.GetType();
- foreach (PropertyInfo propertyInfo in recipeType.GetProperties())
- {
- if (propertyInfo.Name != "LstUnit")
- {
- dataTable.Rows.Add(propertyInfo.Name, propertyInfo.GetValue(recipeStep));
- }
- }
- ints.Add(dataTable.Rows.Count);
-
- recipeStep.LstUnit.ToList().ForEach(k =>
- {
- //List<dynamic> dynamics = new List<dynamic>();
- Type type = k.GetType();
- ints.Add(type.GetProperties().Length);
- foreach (PropertyInfo propertyInfo in type.GetProperties())
- {
- var name = propertyInfo.Name;
- var value = propertyInfo.GetValue(k);
- //dynamicModel.PropertyName = name;
- //dynamicModel.Property = value;
- dataTable.Rows.Add(name, value);
- }
- });
- dataGrid.ItemsSource = dataTable.DefaultView;
- //});
- return new Tuple<DataGrid, List<int>>(dataGrid, ints);
- }
- private void ChangeDataGridRowColor(DataGrid dataGrid,List<int> ints)
- {
- int count = 0;
- for (int i = 0; i < ints.Count; i++)
- {
- for (int j = 0; j < ints[i]; j++)
- {
- dataGrid.UpdateLayout();
- int index = count + j;
- DataGridRow row = dataGrid.ItemContainerGenerator.ContainerFromItem(dataGrid.Items[index]) as DataGridRow;
- row.Background = solidColorBrushes[i];
- }
- count += ints[i];
- }
- }
- private void ChangeGridRowColor(Grid grid, List<int> ints)
- {
- int count = 0;
- for (int i = 0; i < ints.Count; i++)
- {
- for (int j = 0; j < ints[i]; j++)
- {
- //dataGrid.UpdateLayout();
- int index = count + j;
- //DataGridRow row = dataGrid.ItemContainerGenerator.ContainerFromItem(dataGrid.Items[index]) as DataGridRow;
- //grid.RowDefinitions[index]
- }
- count += ints[i];
- }
- }
- #region 旧代码
- /// <summary>
- /// Open local recipe operation
- /// </summary>
- /// <param name="recipeDefineFilePath"></param>
- //public void OpenLocalRecipe(string recipeVariationName = "")
- //{
- // var dlg = new OpenFileDialog();
- // dlg.DefaultExt = ".rcp";
- // dlg.Filter = "Recipe File (.rcp)|*.rcp|All (*.*)|*.*";
- // if (dlg.ShowDialog() == true)
- // {
- // try
- // {
- // XmlDocument rcpDoc = new XmlDocument();
- // rcpDoc.Load(dlg.FileName);
- // string recipeVariation = recipeVariationName;
- // if (string.IsNullOrEmpty(recipeVariation))
- // {
- // var root = rcpDoc.SelectSingleNode("/TableRecipeData") as XmlElement;
- // recipeVariation = root.Attributes["RecipeVersion"].Value;
- // }
- // var startupPath = System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName;
- // string dir = System.IO.Path.GetDirectoryName(startupPath);
- // List<Aitex.Controls.RecipeCompare.Item> map = CustomXmlSerializer.Deserialize<List<Aitex.Controls.RecipeCompare.Item>>(new System.IO.FileInfo(System.IO.Path.Combine(dir, "map.xml")));
- // Func<Aitex.Controls.RecipeCompare.Item, bool> predicate = item => item.Name.Equals(recipeVariation);
- // System.IO.FileInfo fi = new System.IO.FileInfo(string.Format("{0}\\config\\{1}.xml", dir, map.Any(predicate) ? map.Single(predicate).No : recipeVariation));
- // if (fi.Exists)
- // {
- // XmlDocument doc = new XmlDocument();
- // doc.Load(fi.FullName);
- // string formatXml = doc.SelectSingleNode("/Aitex/TableRecipeFormat").OuterXml;
- // string recipeXml = rcpDoc.SelectSingleNode("/TableRecipeData").OuterXml;
- // //LoadRecipe(formatXml, recipeXml);
- // if (OnLocalRecipeOpened != null)
- // OnLocalRecipeOpened.Invoke(new Tuple<string, string>(recipeVariation, dlg.FileName), null);
- // }
- // else
- // {
- // MessageBox.Show(string.Format("工艺程序版本{0}的描述文件不存在,无法正确识别该版本的工艺程序文件。", recipeVariation), "工艺程序打开失败",
- // MessageBoxButton.OK, MessageBoxImage.Error);
- // }
- // }
- // catch (Exception ex)
- // {
- // System.Diagnostics.Debug.WriteLine(ex.Message);
- // MessageBox.Show(string.Format("open recipe {0} failed,please confirm the recipe file is exist and valid。\r\n{1}", dlg.FileName, ex.Message), "Open recipe failed",
- // MessageBoxButton.OK, MessageBoxImage.Error);
- // }
- // }
- //}
- /// <summary>
- /// Save recipe file
- /// </summary>
- //private void SaveRecipeFile()
- //{
- // //try
- // //{
- // // if (Errors != null && Errors.Count > 0)
- // // {
- // // var ret = MessageBox.Show(string.Format("Recipe have {0} error,continue save?", Errors.Count), "Save", MessageBoxButton.YesNo, MessageBoxImage.Warning, MessageBoxResult.No);
- // // if (ret != MessageBoxResult.Yes)
- // // return;
- // // }
- // // if (OnSaveRecipeFile != null)
- // // {
- // // IsRecipeModified = false;
- // // }
- // //}
- // //catch (Exception ex)
- // //{
- // // System.Diagnostics.Debug.WriteLine(ex.Message);
- // // MessageBox.Show("save recipe failed!\r\n\r\n" + ex.Message, "Save", MessageBoxButton.OK, MessageBoxImage.Warning);
- // //}
- //}
- /// <summary>
- /// calc recipe total time
- /// </summary>
- private void CalcRecipeTime()
- {
- //int timeStepRowId = -1;
- //int loopStepRowId = -1;
- //for (int i = 0; i < RecipeRows.Count; i++)
- //{
- // if (RecipeRows[i].TechnicalName == "Time")
- // timeStepRowId = i;
- // if (RecipeRows[i].TechnicalName == "Loop")
- // loopStepRowId = i;
- // if (loopStepRowId != -1 && timeStepRowId != -1)
- // break;
- //}
- //TimeSpan tspan = new TimeSpan();
- //for (int stepNo = 0; stepNo < RecipeRows[timeStepRowId].RecipeItems.Count; stepNo++)
- //{
- // string loopStr = RecipeRows[loopStepRowId].RecipeItems[stepNo].Value;
- // bool isLoopStart = Regex.IsMatch(loopStr, @"^Loop\x20\d+$");
- // if (isLoopStart)
- // {
- // int loopNum = int.Parse(loopStr.ToLower().Replace("loop", "").Replace(" ", ""));
- // TimeSpan ts = new TimeSpan();
- // for (int innerStepNo = stepNo; innerStepNo < RecipeRows[timeStepRowId].RecipeItems.Count; innerStepNo++)
- // {
- // loopStr = RecipeRows[loopStepRowId].RecipeItems[innerStepNo].Value;
- // stepNo = innerStepNo;
- // string timeDuration = RecipeRows[timeStepRowId].RecipeItems[innerStepNo].Value;
- // string[] timeArr = timeDuration.Split(new char[] { ':' }, StringSplitOptions.RemoveEmptyEntries);
- // if (timeArr.Length == 3)
- // {
- // int h, mi, s;
- // if (int.TryParse(timeArr[0], out h) && int.TryParse(timeArr[1], out mi) && int.TryParse(timeArr[2], out s))
- // {
- // var tt = new TimeSpan(h, mi, s);
- // ts += tt;
- // }
- // }
- // bool isLoopEnd = Regex.IsMatch(loopStr, @"^Loop End$");
- // if (isLoopEnd)
- // {
- // tspan += new TimeSpan(ts.Ticks * loopNum);
- // break;
- // }
- // }
- // }
- // else
- // {
- // string timeDuration = RecipeRows[timeStepRowId].RecipeItems[stepNo].Value;
- // string[] timeArr = timeDuration.Split(new char[] { ':' }, StringSplitOptions.RemoveEmptyEntries);
- // if (timeArr.Length == 3)
- // {
- // int h, mi, s;
- // if (int.TryParse(timeArr[0], out h) && int.TryParse(timeArr[1], out mi) && int.TryParse(timeArr[2], out s))
- // {
- // var ts = new TimeSpan(h, mi, s);
- // tspan += ts;
- // }
- // }
- // }
- //}
- //RecipeInfo = string.Format("共{0}步,总时间{1}:{2}:{3}", RecipeRows[0].RecipeItems.Count, (int)tspan.TotalHours, tspan.Minutes.ToString("00"), tspan.Seconds.ToString("00"));
- //InvokePropertyChanged("RecipeInfo");
- }
- #region INotifyPropertyChanged
- public event PropertyChangedEventHandler PropertyChanged;
- public void InvokePropertyChanged(string propertyName)
- {
- if (PropertyChanged != null)
- {
- PropertyChanged.Invoke(this, new PropertyChangedEventArgs(propertyName));
- }
- }
- #endregion INotifyPropertyChanged
- }
- //public class RecipeRow
- //{
- // //private ObservableCollection<SmartCellData> _recipeItems = new ObservableCollection<SmartCellData>();
- // //public RecipeRow(params SmartCellData[] vars)
- // //{
- // // foreach (var var in vars)
- // // _recipeItems.Add(var);
- // //}
- // //public string CatalogName { get; set; }
- // //public string FriendlyName { get; set; }
- // //public string TechnicalName { get; set; }
- // //public ObservableCollection<SmartCellData> RecipeItems
- // //{
- // // get { return _recipeItems; }
- // // set { _recipeItems = value; }
- // //}
- //}
- ///// <summary>
- ///// Recipe head
- ///// </summary>
- //public class RecipeHead
- //{
- // //public string RecipeVariation { get; set; }
- // //public string CreationTime { get; set; }
- // //public string LastRevisionTime { get; set; }
- // //public string CreatedBy { get; set; }
- // //public string LastModifiedBy { get; set; }
- // //public string PressureMode { get; set; }
- // //public string Description { get; set; }
- // //public string BasePressure
- // //{
- // // get; set;
- // //}
- // //public string PumpDownLimit
- // //{
- // // get; set;
- // //}
- // //public string PurgeActive
- // //{
- // // get; set;
- // //}
- // //public string NotToPurgeOrVent
- // //{
- // // get; set;
- // //}
- // //public string Barcode
- // //{
- // // get; set;
- // //}
- // //public string SubstrateTemp
- // //{
- // // get; set;
- // //}
- // //public string PumpingPinState
- // //{
- // // get; set;
- // //}
- // //public string VentingPinState
- // //{
- // // get; set;
- // //}
- // //public string PinDownPressure
- // //{
- // // get; set;
- // //}
- //}
- //public class EndPointConfigItem : INotifyPropertyChanged
- //{
- // public event PropertyChangedEventHandler PropertyChanged;
- // public void InvokePropertyChanged(string propertyName)
- // {
- // if (PropertyChanged != null)
- // {
- // PropertyChanged.Invoke(this, new PropertyChangedEventArgs(propertyName));
- // }
- // }
- // //public string ExposureTime { get; set; }
- // //public string WaveLengthA { get; set; }
- // //public string BinningA { get; set; }
- // //public string WaveLengthB { get; set; }
- // //public string BinningB { get; set; }
- // //public string WaveLengthC { get; set; }
- // //public string BinningC { get; set; }
- // //public string WaveLengthD { get; set; }
- // //public string BinningD { get; set; }
- // //public string Fd { get; set; }
- // //public string PrefilterTime { get; set; }
- // //public string PostfilterTime { get; set; }
- // //public string AlgorithmType { get; set; }
- // //public string Criteria { get; set; }
- // //public string DelayTime { get; set; }
- // //public string ValidationTime { get; set; }
- // //public string ValidationValue { get; set; }
- // //public string TimeWindow { get; set; }
- // //public string MinimalTime { get; set; }
- // //public string PostponeTime { get; set; }
- // //public bool Control { get; set; }
- // //public bool Normalization { get; set; }
- // //public bool EnablePostponePercent { get; set; }
- // //public bool EnableCriterialPercent { get; set; }
- // //public bool EnableEventTrigger { get; set; }
- // //public bool IsFaultIfNoTrigger { get; set; }
- // //public string ToValue()
- // //{
- // // return
- // // $@"ExposureTime={ExposureTime};WaveLengthA={WaveLengthA};BinningA={BinningA};WaveLengthB={WaveLengthB};" +
- // // $@"BinningB={BinningB};WaveLengthC={WaveLengthC};BinningC={BinningC};WaveLengthD={WaveLengthD};BinningD={BinningD};Fd={Fd};" +
- // // $@"PrefilterTime={PrefilterTime};PostfilterTime={PostfilterTime};AlgorithmType={AlgorithmType};Criteria={Criteria};DelayTime={DelayTime};ValidationTime={ValidationTime};" +
- // // $@"ValidationValue={ValidationValue};TimeWindow={TimeWindow};MinimalTime={MinimalTime};PostponeTime={PostponeTime};Control={Control};Normalization={Normalization};" +
- // // $@"EnablePostponePercent={EnablePostponePercent};EnableCriterialPercent={EnableCriterialPercent};EnableEventTrigger={EnableEventTrigger};IsFaultIfNoTrigger={IsFaultIfNoTrigger};"
- // // ;
- // //}
- // //public void SetValue(string config)
- // //{
- // // if (string.IsNullOrEmpty(config))
- // // return;
- // // string[] items = config.Split(';');
- // // foreach (var item in items)
- // // {
- // // if (string.IsNullOrEmpty(item))
- // // continue;
- // // string[] pairs = item.Split('=');
- // // if (pairs.Length != 2)
- // // continue;
- // // Parallel.ForEach(this.GetType().GetProperties(),
- // // property =>
- // // {
- // // PropertyInfo pi = (PropertyInfo) property;
- // // if (pi.Name == pairs[0])
- // // {
- // // try
- // // {
- // // var convertedValue = Convert.ChangeType(pairs[1], pi.PropertyType);
- // // var originValue = Convert.ChangeType(pi.GetValue(this, null), pi.PropertyType);
- // // if (originValue != convertedValue)
- // // {
- // // pi.SetValue(this, convertedValue, null);
- // // }
- // // }
- // // catch (Exception)
- // // {
- // // }
- // // }
- // // });
- // // }
- // //}
- //}
- #endregion
- }
|