![](/img/trans.png)
[英]Multi-line string in PropertyGrid (Extended WPF toolkit) without xaml?
[英]Multi-line string in a PropertyGrid
是否有PropertyGrid
的多行字符串的內置編輯器。
我發現System.Design.dll
有System.ComponentModel.Design.MultilineStringEditor
,可以按如下方式使用:
public class Stuff
{
[Editor(typeof(MultilineStringEditor), typeof(UITypeEditor))]
public string MultiLineProperty { get; set; }
}
不,您需要創建所謂的模態UI類型編輯器。 您需要創建一個繼承自UITypeEditor的類。 這基本上是當您單擊正在編輯的屬性右側的省略號按鈕時顯示的表單。
我發現的唯一缺點是我需要使用特定屬性修飾特定的字符串屬性。 我必須這樣做一段時間。 我從Chris Sells的一本名為“C#中的Windows窗體編程”的書中獲得了這些信息。
VisualHint有一個名為Smart PropertyGrid.NET的商業屬性網格。
是。 我不太清楚它是如何被調用的,但是看一下像屬性ComboBox這樣的Items屬性編輯器
編輯:從@fryguybob開始,ComboBox.Items使用System.Windows.Forms.Design.ListControlStringCollectionEditor
我們需要編寫自定義編輯器以獲得屬性網格中的多行支持。
這是從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;
}
}
編寫自定義屬性網格並將此Editor屬性應用於屬性
class CustomPropertyGrid
{
private string multiLineStr = string.Empty;
[Editor(typeof(MultiLineTextEditor), typeof(UITypeEditor))]
public string MultiLineStr
{
get { return multiLineStr; }
set { multiLineStr = value; }
}
}
在主窗體中分配此對象
propertyGrid1.SelectedObject = new CustomPropertyGrid();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.