Exemplo n.º 1
0
        public CustomPropertyDescriptor CreateProperty(string name, Type type, object value, int index, params Attribute[] attributes)
        {
            var cpd = new CustomPropertyDescriptor(instance, name, type, value, attributes);

            if (index == -1)
            {
                propertyDescriptorList.Add(cpd);
            }
            else
            {
                propertyDescriptorList.Insert(index, cpd);
            }
            TypeDescriptor.Refresh(instance);
            return(cpd);
        }
Exemplo n.º 2
0
            public override PropertyDescriptorCollection GetProperties(ITypeDescriptorContext context, object value, Attribute[] attributes)
            {
                var standardProperties = base.GetProperties(context, value, attributes);
                var obj         = value as CustomObject;
                var customProps = obj == null ? null : obj.Properties;
                var props       = new PropertyDescriptor[standardProperties.Count + (customProps == null ? 0 : customProps.Count)];

                standardProperties.CopyTo(props, 0);
                if (customProps != null)
                {
                    var index = standardProperties.Count;
                    foreach (var property in customProps)
                    {
                        props[index++] = new CustomPropertyDescriptor(property);
                    }
                }
                return(new PropertyDescriptorCollection(props));
            }
Exemplo n.º 3
0
        public override sealed PropertyDescriptorCollection GetProperties()
        {
            if (propertyDescriptorList.Count == 0)
            {
                var pdc = base.GetProperties();  // this gives us a readonly collection, no good
                foreach (PropertyDescriptor pd in pdc)
                {
                    if (!(pd is CustomPropertyDescriptor))
                    {
                        var cpd = new CustomPropertyDescriptor(base.GetPropertyOwner(pd), pd);
                        propertyDescriptorList.Add(cpd);
                    }
                }
            }

            var pdl = propertyDescriptorList.FindAll(pd => pd != null);

            PreProcess(pdl);
            var pdcReturn = new PropertyDescriptorCollection(propertyDescriptorList.ToArray());

            return(pdcReturn);
        }
Exemplo n.º 4
0
        public void SetData(ITypeDescriptorContext context, IWindowsFormsEditorService editorService, object value)
        {
            m_editorService = editorService;

            m_PropertyType = context.PropertyDescriptor.PropertyType;
            if (m_PropertyType.IsEnum)
            {
                m_EnumDataType = Enum.GetUnderlyingType(m_PropertyType);
                m_bFlag        = (m_PropertyType.GetCustomAttributes(typeof(FlagsAttribute), false).Length > 0);
            }

            m_Value = value;

            listViewEnum.Items.Clear( );
            listViewEnum.CheckBoxes = m_bFlag;

            if (!(context.PropertyDescriptor is CustomPropertyDescriptor))
            {
                throw new Exception("PropertyDescriptorManager has not been installed on this instance.");
            }

            CustomPropertyDescriptor cpd = context.PropertyDescriptor as CustomPropertyDescriptor;

            // create list view items for the visible Enum items
            foreach (StandardValueAttribute sva in cpd.StandardValues)
            {
                if (sva.Visible)
                {
                    ListViewItem lvi = new ListViewItem( );
                    lvi.Text      = sva.DisplayName;
                    lvi.ForeColor = (sva.Enabled == true ? lvi.ForeColor : Color.FromKnownColor(KnownColor.GrayText));
                    lvi.Tag       = new TagItem(sva);
                    listViewEnum.Items.Add(lvi);
                }
            }

            UpdateCheckState( );

            // make initial selection
            if (m_bFlag)
            {
                // select the first checked one
                foreach (ListViewItem lvi in listViewEnum.CheckedItems)
                {
                    lvi.Selected = true;
                    lvi.EnsureVisible( );
                    break;
                }
            }
            else
            {
                foreach (ListViewItem lvi in listViewEnum.Items)
                {
                    TagItem ti = lvi.Tag as TagItem;
                    if (ti.Item.Value.Equals(m_Value))
                    {
                        lvi.Selected = true;
                        lvi.EnsureVisible( );
                        break;
                    }
                }
            }
        }
Exemplo n.º 5
0
        private void UpdateEnumDisplayText(CustomPropertyDescriptor cpd)
        {
            if (!(cpd.PropertyType.IsEnum || cpd.PropertyType == typeof(bool)))
            {
                return;
            }
            if ((cpd.PropertyFlags & PropertyFlags.LocalizeEnumerations) <= 0)
            {
                return;
            }
            string                 prefix = String.Empty;
            ResourceManager        rm     = null;
            StandardValueAttribute sva    = null;

            sva = cpd.StandardValues.FirstOrDefault() as StandardValueAttribute;

            // first try property itself
            if (cpd.ResourceManager != null)
            {
                string keyName   = cpd.KeyPrefix + cpd.Name + "_" + sva.Value.ToString() + "_Name";
                string valueName = cpd.ResourceManager.GetString(keyName);
                if (!String.IsNullOrWhiteSpace(valueName))
                {
                    rm     = cpd.ResourceManager;
                    prefix = cpd.KeyPrefix + cpd.Name;
                }
            }

            // now try class level
            if (rm == null && cpd.ResourceManager != null)
            {
                string keyName   = cpd.KeyPrefix + cpd.PropertyType.Name + "_" + sva.Value.ToString() + "_Name";
                string valueName = cpd.ResourceManager.GetString(keyName);
                if (!String.IsNullOrWhiteSpace(valueName))
                {
                    rm     = cpd.ResourceManager;
                    prefix = cpd.KeyPrefix + cpd.PropertyType.Name;
                }
            }

            // try the enum itself if still null
            if (rm == null && cpd.PropertyType.IsEnum)
            {
                var attr = (EnumResourceAttribute)cpd.AllAttributes.FirstOrDefault(a => a is EnumResourceAttribute);
                if (attr != null)
                {
                    try
                    {
                        if (String.IsNullOrWhiteSpace(attr.AssemblyFullName) == false)
                        {
                            rm = new ResourceManager(attr.BaseName, Assembly.ReflectionOnlyLoad(attr.AssemblyFullName));
                        }
                        else
                        {
                            rm = new ResourceManager(attr.BaseName, cpd.PropertyType.Assembly);
                        }
                        prefix = attr.KeyPrefix + cpd.PropertyType.Name;
                    }
                    catch (Exception)
                    {
                        return;
                    }
                }
            }

            if (rm != null)
            {
                foreach (StandardValueAttribute sv in cpd.StandardValues)
                {
                    string keyName     = prefix + "_" + sv.Value.ToString() + "_Name"; // display name
                    string keyDesc     = prefix + "_" + sv.Value.ToString() + "_Desc"; // description
                    string dispName    = String.Empty;
                    string description = String.Empty;

                    try
                    {
                        dispName = rm.GetString(keyName);
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex.Message);
                    }
                    if (String.IsNullOrWhiteSpace(dispName) == false)
                    {
                        sv.DisplayName = dispName;
                    }

                    try
                    {
                        description = rm.GetString(keyDesc);
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex.Message);
                    }
                    if (String.IsNullOrWhiteSpace(description) == false)
                    {
                        sv.Description = description;
                    }
                }
            }
        }
Exemplo n.º 6
0
        public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
        {
            //WriteContext("ConvertFrom", context, value, Type.Missing.GetType());

            ICollection <StandardValueAttribute> col = null;
            Type propType = Type.Missing.GetType();

            if (context != null && context.PropertyDescriptor is CustomPropertyDescriptor)
            {
                CustomPropertyDescriptor cpd = context.PropertyDescriptor as CustomPropertyDescriptor;
                UpdateEnumDisplayText(cpd);
                col      = cpd.StandardValues;
                propType = cpd.PropertyType;
            }
            if (value == null)
            {
                return(null);
            }
            else if (value is string)
            {
                if (propType.IsEnum)
                {
                    var      sInpuValue  = value as string;
                    string[] arrDispName = sInpuValue.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);

                    var sb = new StringBuilder(1000);
                    foreach (string sDispName in arrDispName)
                    {
                        string sTrimValue = sDispName.Trim();
                        foreach (StandardValueAttribute sva in col)
                        {
                            if (String.Compare(sva.Value.ToString(), sTrimValue, true) == 0 ||
                                String.Compare(sva.DisplayName, sTrimValue, true) == 0)
                            {
                                if (sb.Length > 0)
                                {
                                    sb.Append(",");
                                }
                                sb.Append(sva.Value.ToString());
                            }
                        }
                    }  // end of foreach..loop
                    return(Enum.Parse(propType, sb.ToString(), true));
                }
                foreach (StandardValueAttribute sva in col)
                {
                    if (String.Compare(value.ToString(), sva.DisplayName, true, culture) == 0 ||
                        String.Compare(value.ToString(), sva.Value.ToString(), true, culture) == 0)
                    {
                        return(sva.Value);
                    }
                }
                var tc = TypeDescriptor.GetConverter(propType);
                if (tc != null)
                {
                    object convertedValue = null;
                    try
                    {
                        convertedValue = tc.ConvertFrom(context, culture, value);
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex.Message);
                    }
                    if (tc.IsValid(convertedValue))
                    {
                        return(convertedValue);
                    }
                }
            }
            else if (value.GetType() == propType)
            {
                return(value);
            }
            else if (value is StandardValueAttribute)
            {
                return((value as StandardValueAttribute).Value);
            }

            return(base.ConvertFrom(context, culture, value));
        }