|
|
|
@ -0,0 +1,96 @@
|
|
|
|
|
|
|
|
using System;
|
|
|
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
|
|
using System.Linq;
|
|
|
|
|
|
|
|
using System.Reflection;
|
|
|
|
|
|
|
|
using System.Text;
|
|
|
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
using System.Windows.Controls;
|
|
|
|
|
|
|
|
using System.Windows.Data;
|
|
|
|
|
|
|
|
using System.Windows;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
namespace SmartAquaViewer.Classes
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
public static class DataGridAutoBuilder
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
public static void BuildColumnsFromType<T>(DataGrid grid, IEnumerable<T> items,
|
|
|
|
|
|
|
|
Dictionary<string, string> headerMap = null, string dateFormat = "yyyy-MM-dd HH:mm:ss")
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
grid.Columns.Clear();
|
|
|
|
|
|
|
|
var props = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
foreach (var p in props)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
DataGridColumn col = CreateColumnForProperty(p, headerMap, dateFormat);
|
|
|
|
|
|
|
|
if (col != null) grid.Columns.Add(col);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
grid.ItemsSource = items;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private static DataGridColumn CreateColumnForProperty(PropertyInfo p,
|
|
|
|
|
|
|
|
Dictionary<string, string> headerMap, string dateFormat)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
string header = headerMap != null && headerMap.TryGetValue(p.Name, out var h) ? h : p.Name;
|
|
|
|
|
|
|
|
var type = Nullable.GetUnderlyingType(p.PropertyType) ?? p.PropertyType;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// bool → CheckBox
|
|
|
|
|
|
|
|
if (type == typeof(bool))
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
return new DataGridCheckBoxColumn
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
Header = header,
|
|
|
|
|
|
|
|
Binding = new Binding(p.Name) { Mode = BindingMode.TwoWay }
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// enum → ComboBox(읽기/쓰기)
|
|
|
|
|
|
|
|
if (type.IsEnum)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
return new DataGridComboBoxColumn
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
Header = header,
|
|
|
|
|
|
|
|
ItemsSource = Enum.GetValues(type),
|
|
|
|
|
|
|
|
SelectedItemBinding = new Binding(p.Name) { Mode = BindingMode.TwoWay }
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// DateTime → 포맷
|
|
|
|
|
|
|
|
if (type == typeof(DateTime))
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
return new DataGridTextColumn
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
Header = header,
|
|
|
|
|
|
|
|
Binding = new Binding(p.Name)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
Mode = BindingMode.TwoWay,
|
|
|
|
|
|
|
|
StringFormat = dateFormat
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 숫자 → 우측정렬
|
|
|
|
|
|
|
|
if (type == typeof(int) || type == typeof(long) ||
|
|
|
|
|
|
|
|
type == typeof(float) || type == typeof(double) || type == typeof(decimal))
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
var col = new DataGridTextColumn
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
Header = header,
|
|
|
|
|
|
|
|
Binding = new Binding(p.Name) { Mode = BindingMode.TwoWay, UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged }
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
col.ElementStyle = new Style(typeof(TextBlock))
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
Setters = { new Setter(TextBlock.TextAlignmentProperty, TextAlignment.Right) }
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
return col;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 기본(문자 등)
|
|
|
|
|
|
|
|
return new DataGridTextColumn
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
Header = header,
|
|
|
|
|
|
|
|
Binding = new Binding(p.Name) { Mode = BindingMode.TwoWay, UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged }
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|