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);
        }
        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);
        }
Ejemplo n.º 3
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.IsNullOrEmpty(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.IsNullOrEmpty(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.IsNullOrEmpty(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.IsNullOrEmpty(dispName) == false)
                    {
                        sv.DisplayName = dispName;
                    }

                    try
                    {
                        description = rm.GetString(keyDesc);
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex.Message);
                    }
                    if (String.IsNullOrEmpty(description) == false)
                    {
                        sv.Description = description;
                    }
                }
            }
        }
Ejemplo 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;
                    }
                }
            }
        }
Ejemplo n.º 5
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));
        }