自定义FileNameEditor

时间:2012-10-20 15:08:22

标签: c# uitypeeditor

我想实现一个自定义的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.FileNames属性,因为Settings.FileNames的类型为string [],派生类的结果是字符串。 如何告诉派生类返回openFileDialog的FileNames以及如何使过滤器工作?我错过了什么?

3 个答案:

答案 0 :(得分:3)

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

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

答案 1 :(得分:0)

也许使用ArrayEditor作为字符串[]

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

答案 2 :(得分: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);
    }
}