0

我想实现一个自定义 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);

    }
}

这忽略了过滤器属性,虽然我可以选择多个文件,但我无法将它们分配给我的 Settings.FileNames 属性,因为 Settings.FileNames 的类型是字符串 [],而派生类的结果是字符串。如何告诉我的派生类返回 openFileDialog 的 FileNames 以及如何使过滤器工作?我错过了什么?

4

3 回答 3

3

原始代码对我有用,除了需要重新排序。您需要在更改之前调用 base.Initialize,否则它们会被覆盖(调试会很好地显示它)

public class FileNamesEditor : FileNameEditor
{
    protected override void InitializeDialog(OpenFileDialog openFileDialog)
    {
        base.InitializeDialog(openFileDialog);
        openFileDialog.Multiselect = true;
        openFileDialog.Filter = "Word|*.docx|All|*.*";
    }
}
于 2013-09-25T08:24:08.433 回答
0

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

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);
    }
}
于 2012-10-21T00:16:40.300 回答
0

也许对字符串 [] 使用 ArrayEditor

public class Settings
{
  [EditorAttribute(typeof(System.ComponentModel.Design.ArrayEditor),  typeof(System.Drawing.Design.UITypeEditor))]
  public string[] FileNames { get ; set; }
}
于 2012-10-20T15:59:15.023 回答