using OpenTap.Addin.Annotation;
|
using System.Reflection;
|
using System.Windows.Controls;
|
using System.Windows.Data;
|
|
namespace OpenTapEditor.UI
|
{
|
public class DataGridHelper
|
{
|
|
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;
|
}
|
if (property.PropertyType.IsEnum)
|
{
|
dataGrid.Columns.Add(new DataGridComboBoxColumn
|
{
|
Header = attr.Header,
|
IsReadOnly = attr.IsReadOnly,
|
Width = new DataGridLength(1, DataGridLengthUnitType.Star),
|
SelectedItemBinding = new Binding(property.Name),
|
ItemsSource = Enum.GetValues(property.PropertyType)
|
});
|
}
|
else
|
{
|
dataGrid.Columns.Add(new DataGridTextColumn
|
{
|
Header = attr.Header,
|
IsReadOnly = attr.IsReadOnly,
|
Width = new DataGridLength(1, DataGridLengthUnitType.Star),
|
Binding = new Binding(property.Name)
|
});
|
}
|
}
|
}
|
}
|
}
|