// bind custom column (non-auto generated) internal void BindColumn(Column c) { var b = c.Binding; if (b != null) { // get path from binding (may be null) var path = b.Path != null ? b.Path.Path : string.Empty; // get PropertyInfo from binding PropertyInfo cpi = null; if (_props != null && b.Path != null && _props.TryGetValue(path, out cpi)) { c.PropertyInfo = cpi; if (c.PropertyInfo == null && (c.DataType == null || c.DataType == typeof(object))) { c.DataType = cpi.PropertyType; } } // set column name if empty if (string.IsNullOrEmpty(c.ColumnName)) { c.ColumnName = cpi != null ? cpi.Name : path; } } }
// initialize auto column properties based on data type void InitializeAutoColumn(Column c, Type type) { // save column type c.DataType = type; // handle nullable types type = type.GetNonNullableType(); // initialize column properties based on type if (type == typeof(string)) { c.Width = new GridLength(180); } else if (type.IsNumericIntegral()) { c.Width = new GridLength(80); c.Format = "n0"; } else if (type.IsNumericNonIntegral()) { c.Width = new GridLength(80); c.Format = "n2"; } else if (type == typeof(bool)) { c.Width = new GridLength(60); } else if (type == typeof(DateTime)) { c.Format = "d"; } }
void BindAutoColumn(Column c, Type type) { c.Header = type != null ? type.Name : null; var b = new Binding(); if (c.BoundPropertyName == null) { b.Mode = BindingMode.OneWay; } c.Binding = b; InitializeAutoColumn(c, type); }
void BindAutoColumn(Column c, PropertyInfo cpi) { // create automatic binding (property name may be enclosed in square brackets) var name = cpi.Name; List<char> specialChar = new List<char>() { '/', '.' }; bool containsSpecialChar = specialChar.Any(letter => name.Contains(letter)); if (name != null && name.StartsWith("[") && name.EndsWith("]") && !containsSpecialChar) { name = name.Substring(1, name.Length - 2); } var b = new Binding { Path = new PropertyPath(name) }; b.Mode = !cpi.CanWrite ? BindingMode.OneWay : BindingMode.TwoWay; // assign name, binding, property info to column (pi after b!!!!) c.ColumnName = cpi.Name; c.Binding = b; c.PropertyInfo = cpi; // initialize alignment, format, etc InitializeAutoColumn(c, cpi.PropertyType); }
void GenerateColumns(Dictionary<string, PropertyInfo> props) { if (props.Count == 0 && _view != null) { // special case: binding directly to primitive types (int, string, etc) var type = GetItemType(_view); if (Util.Util.IsPrimitive(type)) { var col = new Column(); BindAutoColumn(col, type); col.AutoGenerated = true; Columns.Add(col); } } else { // generate one column for each property foreach (var cpi in props.Values) { var col = new Column(); BindAutoColumn(col, cpi); col.AutoGenerated = true; Columns.Add(col); } } }
public SortingColumnEventArgs(Column col) { Column = col; }