C# 自定义文件名编辑器

C# 自定义文件名编辑器,c#,uitypeeditor,C#,Uitypeeditor,我想实现一个自定义文件名编辑器;我想设置我自己的过滤器,我想能够选择多个文件 public class Settings { [EditorAttribute(typeof(FileNamesEditor), typeof(System.Drawing.Design.UITypeEditor))] public string FileNames { get; set; } } public class FileNamesEditor : FileNameEditor {

我想实现一个自定义文件名编辑器;我想设置我自己的过滤器,我想能够选择多个文件

public class Settings
{
    [EditorAttribute(typeof(FileNamesEditor), typeof(System.Drawing.Design.UITypeEditor))]
    public string FileNames { get; set; }
}

public class FileNamesEditor : FileNameEditor
{
    protected override void InitializeDialog(OpenFileDialog openFileDialog)
    {
        openFileDialog.Multiselect = true;
        openFileDialog.Filter = "Word|*.docx|All|*.*";
        base.InitializeDialog(openFileDialog);

    }
}
这将忽略filter属性,尽管我可以选择多个文件,但无法将它们分配给Settings.FileName属性,因为Settings.FileName的类型为string[],派生类的结果为string。
如何告诉派生类返回openFileDialog的文件名,以及如何使过滤器工作?我错过了什么

可能对字符串[]使用ArrayEditor

public class Settings
{
  [EditorAttribute(typeof(System.ComponentModel.Design.ArrayEditor),  typeof(System.Drawing.Design.UITypeEditor))]
  public string[] FileNames { get ; set; }
}

可能对字符串[]使用ArrayEditor

public class Settings
{
  [EditorAttribute(typeof(System.ComponentModel.Design.ArrayEditor),  typeof(System.Drawing.Design.UITypeEditor))]
  public string[] FileNames { get ; set; }
}

好吧,这就是它的工作原理

public class FileNamesEditor : UITypeEditor
{
    private OpenFileDialog ofd;
    public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context)
    {
        return UITypeEditorEditStyle.Modal;
    }
    public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value)
    {
        if ((context != null) && (provider != null))
        {
            IWindowsFormsEditorService editorService =
            (IWindowsFormsEditorService)
            provider.GetService(typeof(IWindowsFormsEditorService));
            if (editorService != null)
            {
                ofd = new OpenFileDialog();
                ofd.Multiselect = true;
                ofd.Filter = "Word|*.docx|All|*.*";
                ofd.FileName = "";
                if (ofd.ShowDialog() == DialogResult.OK)
                {
                    return ofd.FileNames;
                }
            }
        }
        return base.EditValue(context, provider, value);
    }
}

好吧,这就是它的工作原理

public class FileNamesEditor : UITypeEditor
{
    private OpenFileDialog ofd;
    public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context)
    {
        return UITypeEditorEditStyle.Modal;
    }
    public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value)
    {
        if ((context != null) && (provider != null))
        {
            IWindowsFormsEditorService editorService =
            (IWindowsFormsEditorService)
            provider.GetService(typeof(IWindowsFormsEditorService));
            if (editorService != null)
            {
                ofd = new OpenFileDialog();
                ofd.Multiselect = true;
                ofd.Filter = "Word|*.docx|All|*.*";
                ofd.FileName = "";
                if (ofd.ShowDialog() == DialogResult.OK)
                {
                    return ofd.FileNames;
                }
            }
        }
        return base.EditValue(context, provider, value);
    }
}

最初的代码对我有效,只是需要重新订购。您需要在更改之前调用base.Initialize,否则更改将被覆盖(调试将很好地显示)


最初的代码对我有效,只是需要重新订购。您需要在更改之前调用base.Initialize,否则更改将被覆盖(调试将很好地显示)


我真的不明白你的问题,对话框有一个名为FileNames的属性,你可以对它进行遍历。我也不理解设置类。FileNames属性中必须包含哪些内容?要将文件以逗号分隔吗?settings类将包含多个设置,如要计算的输入文件、辅助程序集的路径、输出格式选项、全球化设置和其他。。。它将在保存设置时序列化,并在每个程序启动时加载,以保存用户在应用程序的每个启动时选择这些设置。应该可以使用OpenFileDialog选择输入文件。。。设置是一个显示在propertygrid中的泛型类。我不太理解你的问题,对话框中有一个名为FileNames的属性,你可以对它进行遍历。我也不理解设置类。FileNames属性中必须包含哪些内容?要将文件以逗号分隔吗?settings类将包含多个设置,如要计算的输入文件、辅助程序集的路径、输出格式选项、全球化设置和其他。。。它将在保存设置时序列化,并在每个程序启动时加载,以保存用户在应用程序的每个启动时选择这些设置。应该可以使用OpenFileDialog选择输入文件。。。设置是一个显示在propertygrid中的通用类。我想将FileNameEditor用作UITypeEditor,但需要将我的设置应用于它。查看我想将FileNameEditor用作UITypeEditor,但需要将我的设置应用于它。查看正确答案,并将其与[Editor(typeof(FileNameEditor)、typeof(UITypeEditor))一起使用]正确答案,并与[Editor(typeof(FileNamesEditor)、typeof(UITypeEditor))一起使用]