alielie
2026-09-10 f3889f2d36c50cf99468594854def9b7ed069fb5
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
using OpenTap;
using OpenTap.Addin.Annotation;
using System.Reflection;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Markup;
 
namespace OpenTapEditor.UI
{
    public class DataGridHelper
    {
        private static readonly DataGridLengthConverter converter = new DataGridLengthConverter();
 
        public static void SetDataGridColumns(DataGrid dataGrid, Type targetType)
        {
            if (targetType == null)
            {
                return;
            }
            PropertyInfo[] properties = targetType.GetProperties(BindingFlags.Instance | BindingFlags.Public);
 
            foreach (var property in properties)
            {
                var attr = property.GetCustomAttribute<DataGridColAttribute>();
                if (attr == null)
                {
                    continue;
                }
                TextBlock tb = new TextBlock();
                tb.SetValue(TextBlock.TextProperty, new DynamicResourceExtension(attr.Header).ProvideValue(null));
                if (property.PropertyType.IsEnum)
                {
                    var col = new DataGridComboBoxColumn
                    {
                        IsReadOnly = attr.IsReadOnly,
                        Header = tb,
                        Width = (DataGridLength)converter.ConvertFromString(attr.Width),
                        SelectedItemBinding = new Binding(property.Name),
                        ItemsSource = Enum.GetValues(property.PropertyType),
                        ElementStyle = dataGrid.Resources.FindName("CellBlock") as Style
                    };
                    dataGrid.Columns.Add(col);
                }
                else if (property.PropertyType == typeof(bool) && !attr.Parameterized)
                {
                    string xaml = @$"
<DataTemplate xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation' >
    <CheckBox VerticalContentAlignment='Center' IsChecked='{{Binding {property.Name},Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}}' IsEnabled='{!attr.IsReadOnly}'/>
</DataTemplate>";
                    var col = new DataGridTemplateColumn
                    {
                        Header = tb,
                        IsReadOnly = true,
                        Width = (DataGridLength)converter.ConvertFromString(attr.Width),
                        CellTemplate = (DataTemplate)XamlReader.Parse(xaml),
                    };
                    dataGrid.Columns.Add(col);
                }
                else
                {
                    string xaml = @$"
<DataTemplate xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation' >
    <TextBlock Style='{{StaticResource CellBlock}}' Text='{{Binding {property.Name}}}'/>
</DataTemplate>";
                    if (attr.IsReadOnly)
                    {
                        var col = new DataGridTemplateColumn
                        {
                            Header = tb,
                            IsReadOnly = attr.IsReadOnly,
                            Width = (DataGridLength)converter.ConvertFromString(attr.Width),
                            CellTemplate = (DataTemplate)XamlReader.Parse(xaml),
                        };
                        dataGrid.Columns.Add(col);
                    }
                    else
                    {
                        if (attr.Parameterized)
                        {
                            string editXaml = @$"
<DataTemplate xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation' 
xmlns:local='clr-namespace:OpenTapEditor;assembly={Assembly.GetExecutingAssembly().GetName().Name}'>
    <local:VariableTextBox Value='{{Binding {property.Name},Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}}' 
Step='{{Binding Step,RelativeSource={{RelativeSource AncestorType=local:StepSettingView,AncestorLevel=1}}}}'
Seq='{{Binding Seq,RelativeSource={{RelativeSource AncestorType=local:StepSettingView,AncestorLevel=1}}}}' />
</DataTemplate>";
                            var col = new DataGridTemplateColumn
                            {
                                Header = tb,
                                IsReadOnly = false,
                                Width = (DataGridLength)converter.ConvertFromString(attr.Width),
                                CellTemplate = (DataTemplate)XamlReader.Parse(xaml),
                                CellEditingTemplate = (DataTemplate)XamlReader.Parse(editXaml),
                            };
                            dataGrid.Columns.Add(col);
                        }
                        else
                        {
                            string boxXaml = @$"
<DataTemplate xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation' >
    <TextBox Style='{{StaticResource CellBox}}' Text='{{Binding {property.Name}}}'/>
</DataTemplate>";
                            var col = new DataGridTemplateColumn
                            {
                                Header = tb,
                                IsReadOnly = false,
                                Width = (DataGridLength)converter.ConvertFromString(attr.Width),
                                CellTemplate = (DataTemplate)XamlReader.Parse(xaml),
                                CellEditingTemplate = (DataTemplate)XamlReader.Parse(boxXaml),
                            };
                            dataGrid.Columns.Add(col);
                        }
                    }
                }
            }
        }
    }
}