private static BindingBase ConstructBinding(Intellibox source, BindingBase template, string propertyName) { if (source == null) { throw new ArgumentNullException("source"); } if (string.IsNullOrEmpty(propertyName)) { throw new ArgumentOutOfRangeException("propertyName", "Cannot be null or empty."); } if (template == null) { return new Binding(propertyName) { Source = source } } ; if (!(template is Binding) && !(template is MultiBinding) && !(template is PriorityBinding)) { throw new ArgumentOutOfRangeException("template", "Unrecognized BindingBase: " + template.GetType().ToString()); } var bind = CloneHelper.Clone(template); if (bind is Binding) { ValidateAndSetSource(source, bind as Binding, propertyName); } else if (bind is MultiBinding) { var a = bind as MultiBinding; // all of these binding have already been cloned, so its safe to set them back to themselves for (int i = 0; i < a.Bindings.Count; i++) { a.Bindings[i] = ConstructBinding(source, a.Bindings[i], propertyName); } } else if (bind is PriorityBinding) { var a = bind as PriorityBinding; // all of these binding have already been cloned, so its safe to set them back to themselves for (int i = 0; i < a.Bindings.Count; i++) { a.Bindings[i] = ConstructBinding(source, a.Bindings[i], propertyName); } } return(bind); }
private GridView ConstructGridView(object item) { var view = new GridView(); bool isBaseType = IsBaseType(item); if (isBaseType || HideColumnHeaders) { view.ColumnHeaderContainerStyle = ZeroHeightColumnHeader; } if (isBaseType) { var gvc = new GridViewColumn(); gvc.Header = item.GetType().Name; gvc.DisplayMemberBinding = new Binding(); view.Columns.Add(gvc); return(view); } if (ExplicitlyIncludeColumns && Columns != null && Columns.Count > 0) { foreach (var col in Columns.Where(c => !c.Hide).OrderBy(c => c.Position ?? int.MaxValue)) { view.Columns.Add(CloneHelper.Clone(col)); } return(view); } var typeProperties = (from p in item.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance) where p.CanRead && p.CanWrite select new { Name = p.Name, Column = Columns.FirstOrDefault(c => p.Name.Equals(c.ForProperty)) }).ToList(); //This is a shortcut to sort the nulls to the back of the list instead of the front //we did this instead of creating an IComparer. var typesWithPositions = typeProperties .Where(a => a.Column != null && a.Column.Position != null).OrderBy(a => a.Column.Position); var typesWithoutPositions = typeProperties.Except(typesWithPositions); var sortedProperties = typesWithPositions.Concat(typesWithoutPositions); foreach (var currentProperty in sortedProperties) { if (currentProperty.Column != null) { if (!currentProperty.Column.Hide) { var gvc = CloneHelper.Clone(currentProperty.Column); if (gvc.Header == null) // TODO check if this is bound to anything { gvc.Header = currentProperty.Name; } if (gvc.DisplayMemberBinding == null) { gvc.DisplayMemberBinding = new Binding(currentProperty.Name); } view.Columns.Add(gvc); } } else { var gvc = new GridViewColumn(); gvc.Header = currentProperty.Name; gvc.DisplayMemberBinding = new Binding(currentProperty.Name); view.Columns.Add(gvc); } } return(view); }