예제 #1
0
        public void SetList(IList items)
        {
            if (items.Count > 0)
            {
                var itemType = items[0].GetType();
                foreach (PropertyDescriptor descriptor in TypeDescriptor.GetProperties(itemType))
                {
                    if (!descriptor.IsBrowsable)
                    {
                        continue;
                    }

                    var cd = new ColumnDefinition { PropertyName = descriptor.Name };

                    if (descriptor.PropertyType == typeof(Color) || descriptor.PropertyType == typeof(bool))
                    {
                        cd.HorizontalAlignment = HorizontalAlignment.Center;
                    }

                    var displayName = descriptor.DisplayName;

                    if (!String.IsNullOrEmpty(displayName))
                        cd.Header = descriptor.DisplayName;

                    MainGrid.ColumnDefinitions.Add(cd);
                }
                MainGrid.ItemsSource = items;
            }
        }
예제 #2
0
        public void SetList(IList items)
        {
            if (items.Count > 0)
            {
                MainGrid.ColumnHeaders = new StringCollection();
                var itemType = items[0].GetType();
                foreach (PropertyDescriptor descriptor in TypeDescriptor.GetProperties(itemType))
                {
                    if (!descriptor.IsBrowsable)
                    {
                        continue;
                    }

                    var cd = new ColumnDefinition() { DataField = descriptor.Name };

                    if (descriptor.PropertyType == typeof(SolidColorBrush))
                    {
                        var colorDisplayTemplate = new DataTemplate { DataType = typeof(SolidColorBrush) };
                        var fef = new FrameworkElementFactory(typeof(Rectangle));
                        fef.SetBinding(Shape.FillProperty, new Binding(descriptor.Name));
                        fef.SetValue(WidthProperty, 12.0);
                        fef.SetValue(HeightProperty, 12.0);
                        fef.SetValue(MarginProperty, new Thickness(4, 0, 4, 0));
                        fef.SetValue(Shape.StrokeThicknessProperty, 1.0);
                        fef.SetValue(Shape.StrokeProperty, Brushes.Gainsboro);
                        colorDisplayTemplate.VisualTree = fef;
                        cd.DisplayTemplate = colorDisplayTemplate;

                        var colorEditTemplate = new DataTemplate { DataType = typeof(SolidColorBrush) };
                        var fefe = new FrameworkElementFactory(typeof(ColorPicker));
                        fefe.SetBinding(ColorPicker.SelectedColorProperty, new Binding(descriptor.Name) { Converter = new BrushToColorConverter() });
                        colorEditTemplate.VisualTree = fefe;
                        cd.EditTemplate = colorEditTemplate;
                    }

                    if (descriptor.PropertyType == typeof(string) && descriptor.Name.Contains("Image"))
                    {
                        var colorEditTemplate = new DataTemplate { DataType = typeof(string) };
                        var fefe = new FrameworkElementFactory(typeof(FilePicker));
                        fefe.SetBinding(FilePicker.FilePathProperty, new Binding(descriptor.Name));
                        colorEditTemplate.VisualTree = fefe;
                        cd.EditTemplate = colorEditTemplate;
                    }

                    var displayName = descriptor.DisplayName;
                    if (!String.IsNullOrEmpty(displayName))
                        cd.Header = descriptor.DisplayName;


                    MainGrid.ColumnDefinitions.Add(cd);
                }
                MainGrid.Content = items;
            }
        }
예제 #3
0
        /// <summary>
        /// Generate column definitions based on a list of items.
        /// </summary>
        /// <param name="list">The list of items.</param>
        /// <returns>A sequence of column definitions.</returns>
        /// <remarks>The constraint is that all the items in the ItemsSource's should be of the same type.
        /// For non built in type, a
        /// <code>public static T Parse(string s, IFormatProvider formatProvider)</code> and
        /// <code>public string ToString(string format, IFormatProvider formatProvider)</code> should be defined.
        /// interface type is not acceptable for no object instance can be created based on it.</remarks>
        protected override IEnumerable<ColumnDefinition> GenerateColumnDefinitions(IList list)
        {
            if (list == null)
            {
                yield break;
            }

            // Strategy 1: get properties from IItemProperties
            var view = CollectionViewSource.GetDefaultView(list);
            var itemPropertiesView = view as IItemProperties;
            if (itemPropertiesView?.ItemProperties != null && itemPropertiesView.ItemProperties.Count > 0)
            {
                foreach (var info in itemPropertiesView.ItemProperties)
                {
                    var descriptor = info.Descriptor as PropertyDescriptor;
                    if (descriptor == null || !descriptor.IsBrowsable)
                    {
                        continue;
                    }

                    var cd = new ColumnDefinition
                    {
                        PropertyName = descriptor.Name,
                        Header = info.Name,
                        HorizontalAlignment = this.DefaultHorizontalAlignment,
                        Width = this.DefaultColumnWidth
                    };

                    yield return cd;
                }

                yield break;
            }

            // Strategy 2: get properties from type descriptor
            var itemType = TypeHelper.FindBiggestCommonType(list);
            var properties = TypeDescriptor.GetProperties(itemType);
            if (properties.Count == 0)
            {
                // Otherwise try to get the property descriptors from an instance
                properties = GetPropertiesFromInstance(list, itemType);
            }

            if (properties.Count > 0)
            {
                foreach (PropertyDescriptor descriptor in properties)
                {
                    if (!descriptor.IsBrowsable)
                    {
                        continue;
                    }

                    var cd = new ColumnDefinition
                    {
                        PropertyName = descriptor.Name,
                        Header = descriptor.Name,
                        HorizontalAlignment = this.DefaultHorizontalAlignment,
                        Width = this.DefaultColumnWidth
                    };

                    yield return cd;
                }

                yield break;
            }

            // Strategy 3: create a single column
            var itemsType = TypeHelper.GetItemType(list);
            yield return
                new ColumnDefinition
                {
                    Header = itemsType.Name,
                    HorizontalAlignment = this.DefaultHorizontalAlignment,
                    Width = this.DefaultColumnWidth
                };
        }
예제 #4
0
        public void SetList(IList items)
        {
            if (items.Count > 0)
            {
                MainGrid.ColumnHeaders = new StringCollection();
                var itemType = items[0].GetType();
                foreach (PropertyDescriptor descriptor in TypeDescriptor.GetProperties(itemType))
                {
                    if (!descriptor.IsBrowsable)
                    {
                        continue;
                    }

                    var cd = new ColumnDefinition()
                    {
                        DataField = descriptor.Name
                    };

                    if (descriptor.PropertyType == typeof(SolidColorBrush))
                    {
                        var colorDisplayTemplate = new DataTemplate {
                            DataType = typeof(SolidColorBrush)
                        };
                        var fef = new FrameworkElementFactory(typeof(Rectangle));
                        fef.SetBinding(Shape.FillProperty, new Binding(descriptor.Name));
                        fef.SetValue(WidthProperty, 12.0);
                        fef.SetValue(HeightProperty, 12.0);
                        fef.SetValue(MarginProperty, new Thickness(4, 0, 4, 0));
                        fef.SetValue(Shape.StrokeThicknessProperty, 1.0);
                        fef.SetValue(Shape.StrokeProperty, Brushes.Gainsboro);
                        colorDisplayTemplate.VisualTree = fef;
                        cd.DisplayTemplate = colorDisplayTemplate;

                        var colorEditTemplate = new DataTemplate {
                            DataType = typeof(SolidColorBrush)
                        };
                        var fefe = new FrameworkElementFactory(typeof(ColorPicker));
                        fefe.SetBinding(ColorPicker.SelectedColorProperty, new Binding(descriptor.Name)
                        {
                            Converter = new BrushToColorConverter()
                        });
                        colorEditTemplate.VisualTree = fefe;
                        cd.EditTemplate = colorEditTemplate;
                    }

                    if (descriptor.PropertyType == typeof(string) && descriptor.Name.Contains("Image"))
                    {
                        var colorEditTemplate = new DataTemplate {
                            DataType = typeof(string)
                        };
                        var fefe = new FrameworkElementFactory(typeof(FilePicker));
                        fefe.SetBinding(FilePicker.FilePathProperty, new Binding(descriptor.Name));
                        colorEditTemplate.VisualTree = fefe;
                        cd.EditTemplate = colorEditTemplate;
                    }

                    var displayName = descriptor.DisplayName;
                    if (!String.IsNullOrEmpty(displayName))
                    {
                        cd.Header = descriptor.DisplayName;
                    }


                    MainGrid.ColumnDefinitions.Add(cd);
                }
                MainGrid.Content = items;
            }
        }
        /// <summary>
        /// Sets the attribute.
        /// </summary>
        /// <param name="attribute">The attribute.</param>
        /// <param name="pi">The pi.</param>
        /// <param name="instance">The instance.</param>
        protected virtual void SetAttribute(Attribute attribute, PropertyItem pi, object instance)
        {
            var ssa = attribute as SelectorStyleAttribute;
            if (ssa != null)
            {
                pi.SelectorStyle = ssa.SelectorStyle;
            }

            var svpa = attribute as SelectedValuePathAttribute;
            if (svpa != null)
            {
                pi.SelectedValuePath = svpa.Path;
            }

            var f = attribute as FontAttribute;
            if (f != null)
            {
                pi.FontSize = f.FontSize;
                pi.FontWeight = f.FontWeight;
                pi.FontFamily = f.FontFamily;
            }

            var fp = attribute as FontPreviewAttribute;
            if (fp != null)
            {
                pi.PreviewFonts = true;
                pi.FontSize = fp.Size;
                pi.FontWeight = fp.Weight;
                pi.FontFamilyPropertyDescriptor = pi.GetDescriptor(fp.FontFamilyPropertyName);
            }

            if (attribute is FontFamilySelectorAttribute)
            {
                pi.IsFontFamilySelector = true;
            }

            var ifpa = attribute as InputFilePathAttribute;
            if (ifpa != null)
            {
                pi.IsFilePath = true;
                pi.IsFileOpenDialog = true;
                pi.FilePathFilter = ifpa.Filter;
                pi.FilePathDefaultExtension = ifpa.DefaultExtension;
            }

            var ofpa = attribute as OutputFilePathAttribute;
            if (ofpa != null)
            {
                pi.IsFilePath = true;
                pi.IsFileOpenDialog = false;
                pi.FilePathFilter = ofpa.Filter;
                pi.FilePathDefaultExtension = ofpa.DefaultExtension;
            }

            if (attribute is DirectoryPathAttribute)
            {
                pi.IsDirectoryPath = true;
            }

            var da = attribute as DataTypeAttribute;
            if (da != null)
            {
                pi.DataTypes.Add(da.DataType);
                switch (da.DataType)
                {
                    case DataType.MultilineText:
                        pi.AcceptsReturn = true;
                        break;
                    case DataType.Password:
                        pi.IsPassword = true;
                        break;
                }
            }

            var ca = attribute as ColumnAttribute;
            if (ca != null)
            {
                var glc = new GridLengthConverter();
                var cd = new ColumnDefinition
                {
                    PropertyName = ca.PropertyName,
                    Header = ca.Header,
                    FormatString = ca.FormatString,
                    Width = (GridLength)(glc.ConvertFromInvariantString(ca.Width) ?? GridLength.Auto),
                    IsReadOnly = ca.IsReadOnly,
                    HorizontalAlignment = StringUtilities.ToHorizontalAlignment(ca.Alignment.ToString(CultureInfo.InvariantCulture))
                };

                // TODO: sort by index
                pi.Columns.Add(cd);
            }

            var la = attribute as ListAttribute;
            if (la != null)
            {
                pi.ListCanAdd = la.CanAdd;
                pi.ListCanRemove = la.CanRemove;
                pi.ListMaximumNumberOfItems = la.MaximumNumberOfItems;
            }

            var ida = attribute as InputDirectionAttribute;
            if (ida != null)
            {
                pi.InputDirection = ida.InputDirection;
            }

            var eia = attribute as EasyInsertAttribute;
            if (eia != null)
            {
                pi.EasyInsert = eia.EasyInsert;
            }

            var sia = attribute as SortIndexAttribute;
            if (sia != null)
            {
                pi.SortIndex = sia.SortIndex;
            }

            var eba = attribute as EnableByAttribute;
            if (eba != null)
            {
                pi.IsEnabledDescriptor = pi.GetDescriptor(eba.PropertyName);
                pi.IsEnabledValue = eba.PropertyValue;
            }

            var vba = attribute as VisibleByAttribute;
            if (vba != null)
            {
                pi.IsVisibleDescriptor = pi.GetDescriptor(vba.PropertyName);
                pi.IsVisibleValue = vba.PropertyValue;
            }

            var oa = attribute as OptionalAttribute;
            if (oa != null)
            {
                pi.IsOptional = true;
                if (oa.PropertyName != null)
                {
                    pi.OptionalDescriptor = pi.GetDescriptor(oa.PropertyName);
                }
            }

            var ila = attribute as IndentationLevelAttribute;
            if (ila != null)
            {
                pi.IndentationLevel = ila.IndentationLevel;
            }

            var ra = attribute as EnableByRadioButtonAttribute;
            if (ra != null)
            {
                pi.RadioDescriptor = pi.GetDescriptor(ra.PropertyName);
                pi.RadioValue = ra.Value;
            }

            if (attribute is CommentAttribute)
            {
                pi.IsComment = true;
            }

            if (attribute is ContentAttribute)
            {
                pi.IsContent = true;
            }

            var ea = attribute as EditableAttribute;
            if (ea != null)
            {
                pi.IsEditable = ea.AllowEdit;
            }

            if (attribute is AutoUpdateTextAttribute)
            {
                pi.AutoUpdateText = true;
            }

            var ispa = attribute as ItemsSourcePropertyAttribute;
            if (ispa != null)
            {
                pi.ItemsSourceDescriptor = pi.GetDescriptor(ispa.PropertyName);
            }

            var liispa = attribute as ListItemItemsSourcePropertyAttribute;
            if (liispa != null)
            {
                var p = TypeDescriptor.GetProperties(instance)[liispa.PropertyName];
                var listItemItemsSource = p != null ? p.GetValue(instance) as IEnumerable : null;
                pi.ListItemItemsSource = listItemItemsSource;
            }

            var clpa = attribute as CheckableItemsAttribute;
            if (clpa != null)
            {
                pi.CheckableItemsIsCheckedPropertyName = clpa.IsCheckedPropertyName;
                pi.CheckableItemsContentPropertyName = clpa.ContentPropertyName;
            }

            var rpa = attribute as BasePathPropertyAttribute;
            if (rpa != null)
            {
                pi.RelativePathDescriptor = pi.GetDescriptor(rpa.BasePathPropertyName);
            }

            var fa = attribute as FilterPropertyAttribute;
            if (fa != null)
            {
                pi.FilterDescriptor = pi.GetDescriptor(fa.PropertyName);
            }

            var dea = attribute as DefaultExtensionPropertyAttribute;
            if (dea != null)
            {
                pi.DefaultExtensionDescriptor = pi.GetDescriptor(dea.PropertyName);
            }

            var fsa = attribute as FormatStringAttribute;
            if (fsa != null)
            {
                pi.FormatString = fsa.FormatString;
            }

            var coa = attribute as ConverterAttribute;
            if (coa != null)
            {
                pi.Converter = Activator.CreateInstance(coa.ConverterType) as IValueConverter;
            }

            var sa = attribute as SlidableAttribute;
            if (sa != null)
            {
                pi.IsSlidable = true;
                pi.SliderMinimum = sa.Minimum;
                pi.SliderMaximum = sa.Maximum;
                pi.SliderSmallChange = sa.SmallChange;
                pi.SliderLargeChange = sa.LargeChange;
                pi.SliderSnapToTicks = sa.SnapToTicks;
                pi.SliderTickFrequency = sa.TickFrequency;
            }

            var spa = attribute as SpinnableAttribute;
            if (spa != null)
            {
                pi.IsSpinnable = true;
                pi.SpinMinimum = spa.Minimum;
                pi.SpinMaximum = spa.Maximum;
                pi.SpinSmallChange = spa.SmallChange;
                pi.SpinLargeChange = spa.LargeChange;
            }

            var wpa = attribute as WidePropertyAttribute;
            if (wpa != null)
            {
                pi.HeaderPlacement = wpa.ShowHeader ? HeaderPlacement.Above : HeaderPlacement.Hidden;
            }

            var wia = attribute as WidthAttribute;
            if (wia != null)
            {
                pi.Width = wia.Width;
            }

            var hpa = attribute as HeaderPlacementAttribute;
            if (hpa != null)
            {
                pi.HeaderPlacement = hpa.HeaderPlacement;
            }

            var ha = attribute as HorizontalAlignmentAttribute;
            if (ha != null)
            {
                pi.HorizontalAlignment = ha.HorizontalAlignment;
            }

            var hea = attribute as HeightAttribute;
            if (hea != null)
            {
                pi.Height = hea.Height;
                pi.MinimumHeight = hea.MinimumHeight;
                pi.MaximumHeight = hea.MaximumHeight;
                pi.AcceptsReturn = true;
            }

            var fta = attribute as FillTabAttribute;
            if (fta != null)
            {
                pi.FillTab = true;
                pi.AcceptsReturn = true;
            }
        }