Ich verwende ExpandoObject weil ich meinem Objekt zur Laufzeit eine Eigenschaft hinzufügen muss.
Grundsätzlich wählt der Benutzer einen „Test“ aus, dann füge ich in meinem Code zwei neue Spalten mit den Namen „X“ und „Y“ hinzu " zu meinem Datagrid. Für jeden Test, den der Benutzer überprüft, fügt mein Code also zwei weitere Spalten mit „X“ und „Y“ hinzu.
Hier ist mein Code:
Code: Select all
public void Fill_Table_Performance()
{
if (main.tab_table.IsSelected && main.Table_performance_AVG != null)
{
list_dynamic_object = new List
{
new List(),new List(),
new List(),new List()
};
List list_count_oos = new List
{
new double(),new double(),new double(),new double()
};
int count = -1;
var List_Test_Num = main.Table_performance_AVG.AsEnumerable()
.GroupBy(r => new { Test_Num = r["Test_Num"] })
.Select(g => g.First())
.ToList();
for (int i = 0; i < List_Table_Performance.Count; i++)
{
count++;
var list_column = List_Table_Performance[i].Columns.Where(x => x.Header.ToString().Contains("X") || x.Header.ToString().Contains("Y")).ToList();
foreach (DataGridTextColumn column in list_column)
{
List_Table_Performance[i].Columns.Remove(column);
}
List_Table_Performance[i].ItemsSource = null;
foreach (DataRow Test_Num in List_Test_Num)
{
var list_column_per_test = List_Table_Performance[i].Columns.Where(x => x.SortMemberPath.ToString() == "X" + int.Parse(Test_Num["Test_Num"].ToString())).ToList();
if (list_column_per_test.Count == 0)
{
Binding Binding_X = new Binding();
Binding_X.Path = new PropertyPath("X" + int.Parse(Test_Num["Test_Num"].ToString()));
Binding_X.IsAsync = true;
Binding Binding_Y = new Binding();
Binding_Y.Path = new PropertyPath("Y" + int.Parse(Test_Num["Test_Num"].ToString()));
Binding_Y.IsAsync = true;
DataGridTextColumn Column_X = new DataGridTextColumn
{
Header = "X",
Binding = Binding_X,
Width = 50,
SortMemberPath = "X" + int.Parse(Test_Num["Test_Num"].ToString())
};
Style columnStyle_X = new Style(typeof(TextBlock));
DataTrigger Trigger_X = new DataTrigger();
Trigger_X.Binding = new Binding("Spec_X" + int.Parse(Test_Num["Test_Num"].ToString()));
Trigger_X.Value = true;
Trigger_X.Setters.Add(
new Setter(
TextBlock.ForegroundProperty,
new SolidColorBrush(Colors.Firebrick)));
columnStyle_X.Triggers.Add(Trigger_X);
Column_X.ElementStyle = columnStyle_X;
DataGridTextColumn Column_Y = new DataGridTextColumn
{
Header = "Y",
Binding = Binding_Y,
Width = 50,
SortMemberPath = "Y" + int.Parse(Test_Num["Test_Num"].ToString())
};
Style columnStyle_Y = new Style(typeof(TextBlock));
DataTrigger Trigger_Y = new DataTrigger();
Trigger_Y.Binding = new Binding("Spec_Y" + int.Parse(Test_Num["Test_Num"].ToString()));
Trigger_Y.Value = true;
Trigger_Y.Setters.Add(
new Setter(
TextBlock.ForegroundProperty,
new SolidColorBrush(Colors.Firebrick)));
columnStyle_Y.Triggers.Add(Trigger_Y);
Column_Y.ElementStyle = columnStyle_Y;
List_Table_Performance[i].Columns.Add(Column_X);
List_Table_Performance[i].Columns.Add(Column_Y);
}
}
foreach (DataRow dt in main.Table_performance_AVG.Rows)
{
List list_date = list_dynamic_object[i].Cast().Where(x => x.Date == ((DateTime)dt["Date"]).ToString("M/d/yyyy HH:mm:ss").ToString()).ToList();
List list_recipe = list_dynamic_object[i].Cast().Where(x => x.Recipe == dt["Recipe_Name"].ToString()).ToList();
if (list_date.Count() == 0 || dt["Test_Num"].ToString() == list_date[0].Test_Num)
{
IDictionary table = new ExpandoObject();
table.Add("Recipe", list_recipe.Any() == false ? dt["Recipe_Name"].ToString() : "");
table.Add("Date", ((DateTime)dt["Date"]).ToString("M/d/yyyy HH:mm:ss").ToString());
table.Add("X" + dt["Test_Num"].ToString(), dt[i + count].ToString() == "-99999" ? null : (double?)dt[i + count]);
table.Add("Y" + dt["Test_Num"].ToString(), dt[i + 1 + count].ToString() == "-99999" ? null : (double?)dt[i + 1 + count]);
table.Add("Spec_X" + dt["Test_Num"].ToString(), false);
table.Add("Spec_Y" + dt["Test_Num"].ToString(), false);
table.Add("Test_Num", dt["Test_Num"].ToString());
if (i > 0)
{
if (dt[List_Table_Performance[i].Uid + "_Spec"].ToString() != "-99999")
{
if (List_Table_Performance[i].Uid == "Test" ?
(double.Parse(table["X" + dt["Test_Num"].ToString()].ToString()) > double.Parse(dt[List_Table_Performance[i].Uid + "_Spec"].ToString()) ||
double.Parse(table["X" + dt["Test_Num"].ToString()].ToString()) < double.Parse(dt[List_Table_Performance[i].Uid + "_Spec"].ToString()) * -1) :
(double.Parse(table["X" + dt["Test_Num"].ToString()].ToString()) > double.Parse(dt[List_Table_Performance[i].Uid + "_Spec"].ToString())
|| double.Parse(table["X" + dt["Test_Num"].ToString()].ToString()) < 0))
{
table["Spec_X" + dt["Test_Num"].ToString()] = true;
}
if (List_Table_Performance[i].Uid == "Test" ?
(double.Parse(table["Y" + dt["Test_Num"].ToString()].ToString()) > double.Parse(dt[List_Table_Performance[i].Uid + "_Spec"].ToString()) ||
double.Parse(table["Y" + dt["Test_Num"].ToString()].ToString()) < double.Parse(dt[List_Table_Performance[i].Uid + "_Spec"].ToString()) * -1) :
(double.Parse(table["Y" + dt["Test_Num"].ToString()].ToString()) > double.Parse(dt[List_Table_Performance[i].Uid + "_Spec"].ToString())
|| double.Parse(table["Y" + dt["Test_Num"].ToString()].ToString()) < 0))
{
table["Spec_Y" + dt["Test_Num"].ToString()] = true;
}
if (bool.Parse(table["Spec_X" + dt["Test_Num"].ToString()].ToString()) == true || bool.Parse(table["Spec_Y" + dt["Test_Num"].ToString()].ToString()) == true)
{
list_count_oos[i]++;
}
}
}
list_dynamic_object[i].Add(table);
}
else
{
int index = list_dynamic_object[i].Cast().ToList().IndexOf(list_date[0]);
IDictionary table_new_test = list_date[0] as IDictionary;
table_new_test.Add("X" + dt["Test_Num"].ToString(), dt[i + count].ToString() == "-99999" ? null : (double?)dt[i + count]);
table_new_test.Add("Y" + dt["Test_Num"].ToString(), dt[i + 1 + count].ToString() == "-99999" ? null : (double?)dt[i + 1 + count]);
//table_ovl1.X1 = main.Table_performance_AVG.Rows[main.Table_performance_AVG.Rows.Count - 1][i].ToString() == "-99999.0" ? null : main.Table_performance_AVG.Rows[main.Table_performance_AVG.Rows.Count - 1][i].ToString();
//table_ovl1.Y1 = main.Table_performance_AVG.Rows[main.Table_performance_AVG.Rows.Count - 1][i].ToString() == "-99999.0" ? null : main.Table_performance_AVG.Rows[main.Table_performance_AVG.Rows.Count - 1][i].ToString();
table_new_test["Test_Num"] = dt["Test_Num"].ToString();
list_dynamic_object[i][index] = table_new_test;
}
}
}
for (int i = 0; i < List_Table_Performance.Count; i++)
{
List_Table_Performance[i].ItemsSource = list_dynamic_object[i];
List_Table_Performance_OOS[i].Text = "Out of Spec: " + Math.Ceiling((list_count_oos[i] / (List_Table_Performance[i].Items.Count)) * 100) + "%";
}
}
}