12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091 |
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.IO;
- using System.Linq;
- using System.Reflection;
- using System.Runtime.Serialization.Formatters.Binary;
- using System.Runtime.Serialization;
- using System.Windows;
- using System.Windows.Controls;
- using System.Windows.Data;
- using System.Windows.Media;
- using Venus_Core;
- using Venus_Core.Attributes;
- using Venus_Unity;
- using Newtonsoft.Json;
- 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;
- private int copyIndex=-1;
-
- public RecipeEditorControlViewModel()
- {
- solidColorBrushes.Add(new SolidColorBrush(Colors.Coral));
- solidColorBrushes.Add(new SolidColorBrush(Colors.Cyan));
- solidColorBrushes.Add(new SolidColorBrush(Colors.Honeydew));
- }
- public void OnAddStep(int index)
- {
- if (CurrentRecipe != null)
- {
- RecipeStep recipeStep;
- if (copyIndex == -1)
- {
- 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-1,recipeStep);
- }
- else
- {
- var t= JsonConvert.SerializeObject(CurrentRecipe);
- recipeStep= Recipe.Load(t).Steps[copyIndex - 1];
- CurrentRecipe.Steps.Insert(index-1, recipeStep);
- }
-
- RecipeStepToGridColumn(recipeStep, currentGrid, index,true );
- for (int i = 1; i <= CurrentRecipe.Steps.Count; i++)
- {
- CurrentRecipe.Steps[i - 1].StepNo = i;
- }
- }
- }
-
- public void OnDeleteStep(int index)
- {
- if (CurrentRecipe != null && CurrentRecipe.Steps.Count>1)
- {
- CurrentRecipe.Steps.RemoveAt(index);
- currentGrid.Children.RemoveRange(currentGrid.RowDefinitions.Count * (index+1), currentGrid.RowDefinitions.Count);
- for (int i = 1; i <= CurrentRecipe.Steps.Count; i++)
- {
- CurrentRecipe.Steps[i - 1].StepNo = i;
- }
- }
- }
- //int index = 0;
- /// <summary>
- /// 选中recipe,显示界面
- /// </summary>
- /// <param name="recipe"></param>
- public void LoadRecipe(Recipe recipe)
- {
- copyIndex = -1;
- 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);
- //index = 0;
- for (int i = 0; i < recipe.Steps.Count; i++)
- {
- RecipeStepToGridColumn(recipe.Steps[i], grid,i,false);
- }
- 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.SetBinding(TextBox.TextProperty, binding);
- 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,int index,bool isInsert)
- {
- int location = 1;
- if (isInsert == true)
- {
- location -= 1;
- }
- Type recipeType = recipeStep.GetType();
-
- int cycleCount = 1;
- if (index == 0)
- {
- cycleCount = 2;
- }
- for (int j = 0; j< cycleCount; j++)
- {
- int i = 0;
- ColumnDefinition col1 = new ColumnDefinition();
- //col1.MinWidth = 50;
- grid.ColumnDefinitions.Insert(index,col1);
- Binding stepcheckbinding = null;
- //grid.MinWidth = 200;
- foreach (PropertyInfo propertyInfo in recipeType.GetProperties())
- {
- string propertyInfoName = propertyInfo.Name;
- string propertyTypeName = propertyInfo.PropertyType.Name;
- if (propertyInfoName != "LstUnit")
- {
- if (index == 0 && grid.ColumnDefinitions.Count == 1)
- {
- RowDefinition row1 = new RowDefinition();
- grid.RowDefinitions.Add(row1);
- }
-
- if (grid.ColumnDefinitions.Count == 1 && j == 0)
- {
- TextBox textBlock = new TextBox();
- textBlock.IsReadOnly = true;
- textBlock.Text = propertyInfoName;
- grid.Children.Add(textBlock);
- textBlock.Background = new SolidColorBrush(Colors.BurlyWood);
- Grid.SetRow(textBlock, i);
- Grid.SetColumn(textBlock, 0);
- }
- else
- {
- 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();
- if (stepcheckbinding == null)
- {
- textBox.IsEnabled = true;
- }
- else
- {
- textBox.SetBinding(TextBox.IsEnabledProperty, stepcheckbinding);
- }
- textBox.SetBinding(TextBox.TextProperty, binding);
- grid.Children.Add(textBox);
- Grid.SetRow(textBox, i);
- Grid.SetColumn(textBox, index + location);
- object[] objAttrs = propertyInfo.GetCustomAttributes(typeof(IsOnlyReadAttribute), true);
- if (objAttrs.Length > 0)
- {
- textBox.IsReadOnly = true;
- ContextMenu contextmenu = new ContextMenu();
- textBox.ContextMenu = contextmenu;
- MenuItem menuItemDelete = new MenuItem();
- menuItemDelete.Header = "删除";
- menuItemDelete.Click += MenuItemDelete_Click;
- MenuItem menuItemLeftInsert = new MenuItem();
- menuItemLeftInsert.Header = "左插入";
- menuItemLeftInsert.Click += MenuItemLeftInsert_Click;
- MenuItem menuItemRightInsert = new MenuItem();
- menuItemRightInsert.Header = "右插入";
- menuItemRightInsert.Click += MenuItemRightInsert_Click;
- MenuItem menuItemCopy = new MenuItem();
- menuItemCopy.Tag = textBox.Text;
- menuItemCopy.Header = "复制";
- menuItemCopy.Click += MenuItemCopy_Click;
- contextmenu.Items.Add(menuItemCopy);
- contextmenu.Items.Add(menuItemDelete);
- contextmenu.Items.Add(menuItemLeftInsert);
- contextmenu.Items.Add(menuItemRightInsert);
- }
- break;
- case "Boolean":
- CheckBox checkBox = new CheckBox();
- checkBox.SetBinding(CheckBox.IsCheckedProperty, binding);
- grid.Children.Add(checkBox);
- Grid.SetRow(checkBox, i);
- Grid.SetColumn(checkBox, index + location);
- if (stepcheckbinding == null)
- {
- stepcheckbinding = new Binding
- {
- Source = checkBox, // 数据源
- Path = new PropertyPath("IsChecked"), // 需绑定的数据源属性名
- Mode = BindingMode.TwoWay, // 绑定模式
- UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged //触发器
- };
- }
- else
- {
- if (stepcheckbinding == null)
- {
- checkBox.IsEnabled = true;
- }
- else
- {
- checkBox.SetBinding(TextBox.IsEnabledProperty, stepcheckbinding);
- }
- }
-
- break;
- default:
- ComboBox comboBox = new ComboBox();
- comboBox.Style = new Style();
- comboBox.SetBinding(ComboBox.SelectedItemProperty, binding);
- ItemsControlHelper.SetEnumValuesToItemsSource(comboBox, true);
- grid.Children.Add(comboBox);
- Grid.SetRow(comboBox, i);
- Grid.SetColumn(comboBox, index + location);
- if (stepcheckbinding == null)
- {
- comboBox.IsEnabled = true;
- }
- else
- {
- comboBox.SetBinding(TextBox.IsEnabledProperty, stepcheckbinding);
- }
- break;
- }
- }
- i++;
- }
- }
- int k = 0;
- recipeStep.LstUnit.ToList().ForEach(x =>
- {
- Type unitType = x.GetType();
- Binding checkbinding = null;
- foreach (PropertyInfo propertyInfo in unitType.GetProperties())
- {
- if (index == 0 && grid.ColumnDefinitions.Count == 1)
- {
- RowDefinition row1 = new RowDefinition();
- grid.RowDefinitions.Add(row1);
- }
- if (grid.ColumnDefinitions.Count == 1 && j == 0)
- {
- TextBox textBlock = new TextBox();
- textBlock.IsReadOnly = true;
- textBlock.Text = propertyInfo.Name;
- textBlock.Background = solidColorBrushes[k % 3];
- grid.Children.Add(textBlock);
- Grid.SetRow(textBlock, i);
- Grid.SetColumn(textBlock, 0);
- }
- else
- {
- 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 //触发器
- };
- var item = propertyInfo.PropertyType.Name;
- if (propertyInfo.Name == "UnitName")
- {
- TextBox textBlock1 = new TextBox();
- textBlock1.Text = propertyInfo.GetValue(x).ToString();
- textBlock1.IsReadOnly = true;
- grid.Children.Add(textBlock1);
- Grid.SetRow(textBlock1, i);
- Grid.SetColumn(textBlock1, index + location);
- }
- else
- {
- switch (item)
- {
- case "Int32":
- case "String":
- TextBox textBox = new TextBox();
- if (checkbinding == null)
- {
- textBox.IsEnabled = true;
- }
- else
- {
- textBox.SetBinding(TextBox.IsEnabledProperty, checkbinding);
- }
- textBox.SetBinding(TextBox.TextProperty, binding);
- grid.Children.Add(textBox);
- Grid.SetRow(textBox, i);
- Grid.SetColumn(textBox, index + location);
- object[] objAttrs = propertyInfo.GetCustomAttributes(typeof(IsOnlyReadAttribute), true);
- if (objAttrs.Length > 0)
- {
- textBox.IsReadOnly = true;
- }
- break;
- case "Boolean":
- CheckBox checkBox = new CheckBox();
- if (checkbinding == null)
- {
- checkbinding = new Binding
- {
- Source = checkBox, // 数据源
- Path = new PropertyPath("IsChecked"), // 需绑定的数据源属性名
- Mode = BindingMode.TwoWay, // 绑定模式
- UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged //触发器
- };
- }
-
- checkBox.SetBinding(CheckBox.IsCheckedProperty, binding);
- grid.Children.Add(checkBox);
- Grid.SetRow(checkBox, i);
- Grid.SetColumn(checkBox, index+location);
- break;
- default:
- ComboBox comboBox = new ComboBox();
- if (checkbinding == null)
- {
- comboBox.IsEnabled = true;
- }
- else
- {
- comboBox.SetBinding(TextBox.IsEnabledProperty, checkbinding);
- }
- comboBox.Style = new Style();
- 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, index + location);
- break;
- }
- }
- }
- i++;
- }
- k++;
- });
- //index++;
- }
-
-
- }
- //private void RecipeStepToGridColumn(RecipeStep recipeStep, Grid grid)
- //{
- // try
- // {
- // 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.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;
- // //textBox.SetBinding(TextBox.TextProperty, currentGrid.ColumnDefinitions.c);
- // ContextMenu contextmenu = new ContextMenu();
- // textBox.ContextMenu = contextmenu;
- // MenuItem menuItemDelete = new MenuItem();
- // menuItemDelete.Header = "删除";
- // menuItemDelete.Click += MenuItemDelete_Click;
- // MenuItem menuItemInsert = new MenuItem();
- // menuItemInsert.Header = "左插入";
- // menuItemInsert.Click += MenuItemInsert_Click;
- // MenuItem menuItemCopy = new MenuItem();
- // menuItemCopy.Tag = textBox.Text;
- // menuItemCopy.Header = "复制";
- // menuItemCopy.Click += MenuItemCopy_Click;
- // contextmenu.Items.Add(menuItemCopy);
- // contextmenu.Items.Add(menuItemDelete);
- // contextmenu.Items.Add(menuItemInsert);
- // }
- // break;
- // case "Boolean":
- // CheckBox checkBox = new CheckBox();
- // 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.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)
- // {
- // TextBox textBlock = new TextBox();
- // textBlock.IsReadOnly = true;
- // textBlock.Text = propertyInfoName;
- // grid.Children.Add(textBlock);
- // textBlock.Background = new SolidColorBrush(Colors.BurlyWood);
- // Grid.SetRow(textBlock, i);
- // Grid.SetColumn(textBlock, 0);
- // }
- // i++;
- // }
- // }
- // List<int> ints = new List<int>();
- // ints.Add(recipeType.GetProperties().Length);
- // int k = 0;
- // recipeStep.LstUnit.ToList().ForEach(x =>
- // {
- // Type unitType = x.GetType();
- // Binding checkbinding = null;
- // 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
- // {
- // //CheckBox check = new CheckBox();
- // //check.IsChecked = false;
- // switch (item)
- // {
- // case "Int32":
- // case "String":
- // TextBox textBox = new TextBox();
- // //textBox.SetBinding(TextBox.IsEnabledProperty, check.IsChecked);
- // //textBox.IsEnabled= true;
- // if (checkbinding == null)
- // {
- // textBox.IsEnabled = true;
- // }
- // else
- // {
- // textBox.SetBinding(TextBox.IsEnabledProperty, checkbinding);
- // }
- // //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();
- // checkbinding = new Binding
- // {
- // Source = checkBox, // 数据源
- // //Path = new PropertyPath($"LstUnit[{k}]." + propertyInfo.Name), // 需绑定的数据源属性名
- // Path = new PropertyPath("IsChecked"), // 需绑定的数据源属性名
- // Mode = BindingMode.TwoWay, // 绑定模式
- // UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged //触发器
- // };
- // //checkbinding = binding;
- // //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();
- // if (checkbinding == null)
- // {
- // comboBox.IsEnabled = true;
- // }
- // else
- // {
- // comboBox.SetBinding(TextBox.IsEnabledProperty, checkbinding);
- // }
- // comboBox.Style = new Style();
- // 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)
- // {
- // TextBox textBlock = new TextBox();
- // textBlock.IsReadOnly = true;
- // 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++;
- // }
- // catch
- // { }
-
- // //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 += MenuItemLeftInsert_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 MenuItemLeftInsert_Click(object sender, RoutedEventArgs e)
- {
- var t = ((sender as MenuItem).Parent as ContextMenu).PlacementTarget as TextBox;
- int insertIndex = Convert.ToInt32(t.Text);
- OnAddStep(insertIndex);
- }
- private void MenuItemRightInsert_Click(object sender, RoutedEventArgs e)
- {
- var t = ((sender as MenuItem).Parent as ContextMenu).PlacementTarget as TextBox;
- int insertIndex = Convert.ToInt32(t.Text);
- OnAddStep(insertIndex+1);
- }
- private void MenuItemDelete_Click(object sender, RoutedEventArgs e)
- {
- var t = ((sender as MenuItem).Parent as ContextMenu).PlacementTarget as TextBox;
- int deleteIndex = Convert.ToInt32(t.Text);
- OnDeleteStep(deleteIndex - 1);
- }
- private void MenuItemCopy_Click(object sender, RoutedEventArgs e)
- {
- var t= ((sender as MenuItem).Parent as ContextMenu).PlacementTarget as TextBox;
- copyIndex =Convert.ToInt32( t.Text);
-
- }
- /// <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);
- }
- }
-
- 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 INotifyPropertyChanged
- public event PropertyChangedEventHandler PropertyChanged;
- public void InvokePropertyChanged(string propertyName)
- {
- if (PropertyChanged != null)
- {
- PropertyChanged.Invoke(this, new PropertyChangedEventArgs(propertyName));
- }
- }
- #endregion INotifyPropertyChanged
- }
- }
|