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);
|
}
|
}
|
}
|
}
|
}
|
}
|
}
|