[英]Custom editors in a property grid that uses Dictionary
I am using a property grid that uses a dictionary using the adapter found here . 我正在使用一个属性网格,该属性网格使用这里找到的适配器的字典。
I now need the ability to use a custom editor. 我现在需要使用自定义编辑器的功能。 More specifically a file chooser.
更具体地说,是文件选择器。 If the object in the dictionary is a string it just uses the default string editor.
如果字典中的对象是字符串,则仅使用默认的字符串编辑器。
Could I implement a new class called FilePath or something that would just act as a wrapper for string but would cause the property grid to use the OpenFileDialog, and display the result as a string in the PropertyGrid once chosen? 我是否可以实现一个称为FilePath的新类,该类仅充当字符串的包装器,但会导致属性网格使用OpenFileDialog,并将结果显示为PropertyGrid中的字符串(一旦选择)?
Is this possible? 这可能吗? And if so how?
如果是这样怎么办?
If you want to have the file path editor in the property grid using the dictionary adapter you have referenced, I would make the FilePath class like you suggest. 如果要使用引用的字典适配器在属性网格中具有文件路径编辑器,我将按照您的建议制作FilePath类。 You will need to also implement two additional classes to make this all work with the property grid: An editor and a type converter.
您还需要实现两个其他类,以使它们全部与属性网格一起使用:编辑器和类型转换器。
Let's assume your FilePath object is a simple one: 假设您的FilePath对象是一个简单的对象:
class FilePath
{
public FilePath(string inPath)
{
Path = inPath;
}
public string Path { get; set; }
}
Your property grid will display the class name in light gray, not very useful. 您的属性网格将以浅灰色显示类名称,不是很有用。 Let's write a TypeConverter to display the string that this class really wraps around
让我们编写一个TypeConverter来显示该类真正包装的字符串
class FilePathConverter : TypeConverter
{
public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
{
if (sourceType == typeof(string))
return true;
return base.CanConvertFrom(context, sourceType);
}
public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
{
if (IsValid(context, value))
return new FilePath((string)value);
return base.ConvertFrom(context, culture, value);
}
public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
{
if (destinationType == typeof(string))
return destinationType == typeof(string);
return base.CanConvertTo(context, destinationType);
}
public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destinationType)
{
if (destinationType == typeof(string))
return ((FilePath)value).Path;
return base.ConvertTo(context, culture, value, destinationType);
}
public override bool IsValid(ITypeDescriptorContext context, object value)
{
if (value.GetType() == typeof(string))
return true;
return base.IsValid(context, value);
}
}
Add the TypeConverter attribute to our FilePath class to convert to and from a string. 将TypeConverter属性添加到我们的FilePath类中,以在字符串之间进行转换。
[TypeConverter(typeof(FilePathConverter))]
class FilePath
{
...
}
Now the property grid will display the string and not the type name, but you want the ellipsis to bring up a file selection dialog, so we make a UITypeEditor: 现在,属性网格将显示字符串而不是类型名称,但是您希望省略号弹出文件选择对话框,因此我们创建了一个UITypeEditor:
class FilePathEditor : UITypeEditor
{
public override System.Drawing.Design.UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context)
{
return System.Drawing.Design.UITypeEditorEditStyle.Modal;
}
public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value)
{
FilePath path = (FilePath)value;
OpenFileDialog openFile = new OpenFileDialog();
openFile.FileName = path.Path;
if (openFile.ShowDialog() == DialogResult.OK)
path.Path = openFile.FileName;
return path;
}
}
Add the Editor attribute to our FilePath class to use the new class: 将Editor属性添加到我们的FilePath类中以使用新类:
[TypeConverter(typeof(FilePathConverter))]
[Editor(typeof(FilePathEditor), typeof(UITypeEditor))]
class FilePath
{
...
}
Now you can add FilePath objects to your IDictionary and have them editable through the property grid 现在,您可以将FilePath对象添加到IDictionary中,并通过属性网格进行编辑
IDictionary d = new Dictionary<string, object>();
d["Path"] = new FilePath("C:/");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.