Multi-line string in a PropertyGrid

后端 未结 4 1961
走了就别回头了
走了就别回头了 2020-12-15 16:23

Is there a built-in editor for a multi-line string in a PropertyGrid.

4条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-15 16:49

    We need to write our custom editor to get the multiline support in property grid.

    Here is the customer text editor class implemented from UITypeEditor

    public class MultiLineTextEditor : UITypeEditor
    {
        private IWindowsFormsEditorService _editorService;
    
        public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context)
        {
            return UITypeEditorEditStyle.DropDown;
        }
    
        public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value)
        {
            _editorService = (IWindowsFormsEditorService)provider.GetService(typeof(IWindowsFormsEditorService));
    
            TextBox textEditorBox = new TextBox();
            textEditorBox.Multiline = true;
            textEditorBox.ScrollBars = ScrollBars.Vertical;
            textEditorBox.Width = 250;
            textEditorBox.Height = 150;
            textEditorBox.BorderStyle = BorderStyle.None;
            textEditorBox.AcceptsReturn = true;
            textEditorBox.Text = value as string;
    
            _editorService.DropDownControl(textEditorBox);
    
            return textEditorBox.Text;
        }
    }
    

    Write your custom property grid and apply this Editor attribute to the property

    class CustomPropertyGrid
    {
        private string multiLineStr = string.Empty;
    
        [Editor(typeof(MultiLineTextEditor), typeof(UITypeEditor))]
        public string MultiLineStr
        {
            get { return multiLineStr; }
            set { multiLineStr = value; }
        }
    }
    

    In main form assign this object

     propertyGrid1.SelectedObject = new CustomPropertyGrid();
    

提交回复
热议问题