aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/FSE/Modules/Tango.FSE.Procedures/Dialogs/ResultGridView.xaml.cs
blob: c16086d9f157081cc3523f3b1098139a7bccce66 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Tango.FSE.Procedures.Dialogs
{
    /// <summary>
    /// Interaction logic for ResultGridView.xaml
    /// </summary>
    public partial class ResultGridView : UserControl
    {
        public class ResultObject
        {
            public Object V0 { get; set; }
            public Object V1 { get; set; }
            public Object V2 { get; set; }
            public Object V3 { get; set; }
            public Object V4 { get; set; }
            public Object V5 { get; set; }
            public Object V6 { get; set; }
            public Object V7 { get; set; }
            public Object V8 { get; set; }
            public Object V9 { get; set; }
            public Object V10 { get; set; }
            public Object V11 { get; set; }
            public Object V12 { get; set; }
            public Object V13 { get; set; }
            public Object V14 { get; set; }
        }

        public ResultGridView()
        {
            InitializeComponent();

            Loaded += ResultGridView_Loaded;
        }

        private void ResultGridView_Loaded(object sender, RoutedEventArgs e)
        {
            var vm = DataContext as ResultGridViewVM;
            var results = vm.Items;

            List<ResultObject> objects = new List<ResultObject>();

            if (results.Count > 0)
            {
                var model = results.First();

                if (model.GetType().IsValueTypeOrString())
                {
                    grid.Columns.Add(new DataGridTextColumn()
                    {
                        Header = "Values",
                        Binding = new Binding("V0"),
                    });

                    foreach (var item in results)
                    {
                        objects.Add(new ResultObject()
                        {
                            V0 = item,
                        });
                    }
                }
                else
                {
                    int columnCount = 0;

                    //Generate columns
                    foreach (var prop in model.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance))
                    {
                        var att = prop.GetCustomAttribute<DescriptionAttribute>();

                        grid.Columns.Add(new DataGridTextColumn()
                        {
                            Header = att != null ? att.Description : prop.Name,
                            Binding = new Binding($"V{columnCount++}"),
                        });
                    }

                    foreach (var field in model.GetType().GetFields(BindingFlags.Public | BindingFlags.Instance))
                    {
                        var att = field.GetCustomAttribute<DescriptionAttribute>();

                        grid.Columns.Add(new DataGridTextColumn()
                        {
                            Header = att != null ? att.Description : field.Name,
                            Binding = new Binding($"V{columnCount++}"),
                        });
                    }

                    //Generate cells

                    foreach (var item in results)
                    {
                        ResultObject obj = new ResultObject();

                        var properties = model.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance).ToList();
                        var fields = model.GetType().GetFields(BindingFlags.Public | BindingFlags.Instance).ToList();

                        int propCount = 0;

                        for (int i = 0; i < properties.Count; i++)
                        {
                            typeof(ResultObject).GetProperty($"V{i}").SetValue(obj, properties[i].GetValue(item));
                            propCount++;
                        }

                        for (int i = 0; i < fields.Count; i++)
                        {
                            typeof(ResultObject).GetProperty($"V{i + propCount}").SetValue(obj, fields[i].GetValue(item));
                        }

                        objects.Add(obj);
                    }
                }
            }

            grid.ItemsSource = objects;
        }
    }
}