Пример #1
0
        /// <summary>
        /// Implements the dropdown style for selecting layers
        /// </summary>
        /// <param name="context"></param>
        /// <param name="provider"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value)
        {
            // Uses the IWindowsFormsEditorService to display a  drop-down UI
            if (edSvc == null)
            {
                edSvc = (IWindowsFormsEditorService)provider.GetService(typeof(IWindowsFormsEditorService));
            }
            if (edSvc != null)
            {
                if (listbox == null)
                {
                    listbox                       = new ListBox();
                    listbox.BorderStyle           = BorderStyle.None;
                    listbox.SelectedIndexChanged += new EventHandler(OnListBoxChanged);
                }
                GraphLayerAttribute attr = context.PropertyDescriptor.Attributes[typeof(GraphLayerAttribute)] as GraphLayerAttribute;

                GraphLayerCollection values = new GraphLayerCollection();



                //attributes are the only way to pass info from the context to the property grid
                //A UITypeDescriptor is supposed to be independent of the context/application
                //There is no way you can get access to the canvas starting from the grid or from this class

                if (attr != null && !attr.IsDefaultAttribute())
                {
                    values.AddRange(attr.Layers);
                }

                //GraphLayer dl = new GraphLayer("Default");
                values.Add(GraphAbstract.DefaultLayer);
                //this is only for design-time support:
                //ISelectionService serv = (ISelectionService )provider.GetService(typeof(ISelectionService ));
                listbox.Items.Clear();

                int  width = 0;
                Font font  = listbox.Font;

                // Add the standard values in the list box and
                // measure the text at the same time.

                using (Graphics g = listbox.CreateGraphics())
                {
                    foreach (GraphLayer layer in values)
                    {
                        if (!listbox.Items.Contains(layer))
                        {
                            //							if (!editor.ShowPreviewOnly)
                            width = (int)Math.Max(width, g.MeasureString(layer.ToString(), font).Width);
                            listbox.Items.Add(layer);
                        }
                    }
                }


                listbox.SelectedItem = value;
                listbox.Height       =
                    Math.Max(font.Height + 2, Math.Min(200, listbox.PreferredHeight));
                listbox.Width = Math.Max(width, 100);

                edSvc.DropDownControl(listbox);

                if (listbox.SelectedItem != null)
                {
                    return(listbox.SelectedItem);
                }
                else
                {
                    return(value);
                }
            }
            return(value);
        }