【问题标题】:Custom editors in a property grid that uses Dictionary使用 Dictionary 的属性网格中的自定义编辑器
【发布时间】:2012-11-28 16:03:03
【问题描述】:

我正在使用一个使用字典的属性网格,该字典使用找到的适配器here

我现在需要能够使用自定义编辑器。更具体地说是文件选择器。如果字典中的对象是一个字符串,它只使用默认的字符串编辑器。

我是否可以实现一个名为 FilePath 的新类或仅充当字符串包装器但会导致属性网格使用 OpenFileDialog,并在选择后将结果作为字符串显示在 PropertyGrid 中的东西?

这可能吗?如果是的话怎么办?

【问题讨论】:

标签: c# .net propertygrid


【解决方案1】:

如果您想使用您引用的字典适配器在属性网格中使用文件路径编辑器,我会按照您的建议制作 FilePath 类。您还需要实现两个额外的类以使这一切都与属性网格一起使用:一个编辑器和一个类型转换器。

假设您的 FilePath 对象是一个简单的对象:

class FilePath
{
    public FilePath(string inPath)
    {
        Path = inPath;
    }

    public string Path { get; set; }
}

您的属性网格将以浅灰色显示类名,不是很有用。让我们写一个 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);
    }
}

将 TypeConverter 属性添加到我们的 FilePath 类以转换为字符串。

[TypeConverter(typeof(FilePathConverter))]
class FilePath
{
    ...
}

现在属性网格将显示字符串而不是类型名称,但您希望省略号显示文件选择对话框,因此我们创建了一个 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;
    }
}

将 Editor 属性添加到我们的 FilePath 类以使用新类:

[TypeConverter(typeof(FilePathConverter))]
[Editor(typeof(FilePathEditor), typeof(UITypeEditor))]
class FilePath
{
    ...
}

现在您可以将 FilePath 对象添加到 IDictionary 并通过属性网格编辑它们

IDictionary d = new Dictionary<string, object>();
d["Path"] = new FilePath("C:/");

【讨论】:

  • 这非常有效。正是我的想法,我只是不知道如何实现转换器和 UI 编辑器类。干杯!
  • +1 不是我想要的,但答案太好了,不能为 0!
  • 这似乎是动态属性的方式。但是看到现有 FileNameEditor 所做的大量代码实在令人痛苦。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-10-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多