------------------------------------
-- 自定义属性编辑器
------------------------------------
自定义属性编辑器
定义一个从 UITypeEditor 派生的类。
重写 EditValue 方法,以处理用户界面、用户输入操作以及值的分配。
重写 GetEditStyle 方法,以便将编辑器将使用的编辑器样式的类型通知给“属性”窗口。
通过执行下列步骤,可以为在“属性”窗口中绘制值的表示形式添加附加支持:
重写 GetPaintValueSupported 方法以指示编辑器支持显示值的表示形式。
重写 PaintValue 方法以实现该值的表示形式的显示。
如果编辑器应具有初始化行为,则重写 UITypeEditor 构造函数方法
保存文件编辑器
/// <summary>
/// 保存文件编辑器
/// </summary>
/// <example>
/// [Editor(typeof(SaveFileEditor), typeof(System.Drawing.Design.UITypeEditor))]
/// public string FilePath { get; set; }
/// </example>
public class SaveFileEditor : UITypeEditor
{
SaveFileDialog _dlg;
public SaveFileEditor()
{
SaveFileDialog dlg = new SaveFileDialog();
dlg.Filter = "All files (*.*)|*.*";
InitializeDialog(dlg);
}
public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value)
{
if (DialogResult.OK == _dlg.ShowDialog())
return _dlg.FileName;
else
return value.ToString();
}
public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context)
{
return UITypeEditorEditStyle.Modal;
}
protected virtual void InitializeDialog(SaveFileDialog saveFileDialog)
{
_dlg = saveFileDialog;
}
}
枚举下拉框编辑器
[Editor(typeof(SourceTypeEditor), typeof(UITypeEditor))]
public SourceType SourceType {get; set;}
--------------------------------------------
public enum SourceType { LAN, WebPage, FTP, eMail, OCR }
--------------------------------------------
/// <summary>
/// 图片枚举下拉框(属性编辑器)
/// </summary>
public class SourceTypeEditor : UITypeEditor
{
public override bool GetPaintValueSupported(ITypeDescriptorContext context)
{
return true;
}
public override void PaintValue(PaintValueEventArgs e)
{
// 获取资源名称
int i = (int)e.Value;
string sourceName = "";
switch (i)
{
case ((int)SourceType.LAN): sourceName = "LANTask"; break;
case ((int)SourceType.WebPage): sourceName = "WebTask"; break;
case ((int)SourceType.FTP): sourceName = "FTPTask"; break;
case ((int)SourceType.eMail): sourceName = "eMailTask"; break;
case ((int)SourceType.OCR): sourceName = "OCRTask"; break;
}
// 加载资源文件
string module = this.GetType().Module.Name;
module = module.Substring(0, module.Length - 4);
ResourceManager resourceManager = new ResourceManager(module + ".ResourceStrings.SampleResources", Assembly.GetExecutingAssembly());
// 获取并绘制图像资源
Bitmap newImage = (Bitmap)resourceManager.GetObject(sourceName);
Rectangle destRect = e.Bounds;
newImage.MakeTransparent();
e.Graphics.DrawImage(newImage, destRect);
}
}
对比度设置编辑器
[Editor(typeof(ContrastEditor), typeof(System.Drawing.Design.UITypeEditor))]
public int Contrast {get; set;}
--------------------------------------------
/// <summary>
/// 对比度编辑器。使用 FrmContrast 窗口来设置值
/// </summary>
public class ContrastEditor : UITypeEditor
{
public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context)
{
return UITypeEditorEditStyle.DropDown;
}
public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value)
{
IWindowsFormsEditorService wfes = provider.GetService(typeof(IWindowsFormsEditorService))
as IWindowsFormsEditorService;
if (wfes != null)
{
FrmContrast frmContrast = new FrmContrast(wfes, (int)value);
wfes.DropDownControl(frmContrast);
value = frmContrast.Value;
}
return value;
}
}
------------------------------------------
/// <summary>
/// Summary description for frmContrast.
/// </summary>
public class FrmContrast : System.Windows.Forms.Form
{
public int Value
{
get { return trackBar.Value; }
set { trackBar.Value = value; }
}
private IWindowsFormsEditorService _editorService;
public FrmContrast(IWindowsFormsEditorService editorService, int value)
{
InitializeComponent();
TopLevel = false;
_editorService = editorService;
Value = value;
btnTrack.Text = Value.ToString();
}
private void frmContrast_Closed(object sender, System.EventArgs e)
{
if (_editorService != null)
_editorService.CloseDropDown();
}
private void trackBar_ValueChanged(object sender, System.EventArgs e)
{
btnTrack.Text = Value.ToString();
}
private void btnTrack_Click(object sender, System.EventArgs e)
{
Close();
}
}
集合编辑器
方法一:用 List<T> 最为简单
public List<Employee> Employees { get; set; }
public class Employee
{
public string FirstName { get; set; }
public string LastName { get; set; }
public override string ToString()
{
return string.Format("{0} {1}", FirstName, LastName);
}
}
方法一:继承CollectionEditor
// 集合类编辑器
public class EmployeeCollectionEditor : CollectionEditor
{
public EmployeeCollectionEditor(Type type) : base(type) {}
protected override string GetDisplayText(object value)
{
Employee item = new Employee();
item = (Employee)value;
return base.GetDisplayText(string.Format("{0}, {1}",item.LastName,item.FirstName));
}
}
// 集合类
public class EmployeeCollection : CollectionBase
{
public Employee this[int index]
{
get { return (Employee)List[index]; }
}
public void Add(Employee emp)
{
List.Add(emp);
}
public void Remove(Employee emp)
{
List.Remove(emp);
}
}
// 类
public class Organization
{
[Editor(typeof(EmployeeCollectionEditor),typeof(System.Drawing.Design.UITypeEditor))]
public EmployeeCollection Employees { get; set;}
}
呵呵 没有