【问题标题】:Custom FileNameEditor自定义文件名编辑器
【发布时间】:2012-10-20 15:08:22
【问题描述】:

我想实现一个自定义的 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 以及如何使过滤器工作?我错过了什么?

【问题讨论】:

  • 我不太明白你的问题,对话框有一个名为 FileNames 的属性,你可以对其进行 foreach 。我也不了解设置类。 FileNames 属性中必须包含什么?想要用逗号分隔文件吗?
  • 设置类将包含多个设置,例如要计算的输入文件、辅助程序集的路径、输出格式选项、全球化设置等...在保存设置时将被序列化并在每个程序中加载start 以防止用户在每次启动应用程序时选择这些。输入文件应该可以使用 OpenFileDialog 进行选择...设置是属性网格中显示的通用类。

标签: c# uitypeeditor


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

    也许对字符串[] 使用 ArrayEditor

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

    【讨论】:

    【解决方案3】:

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

    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);
        }
    }
    

    【讨论】:

    • 正确答案,与[Editor(typeof(FileNamesEditor), typeof(UITypeEditor))]一起使用
    猜你喜欢
    • 1970-01-01
    • 2021-11-27
    • 1970-01-01
    • 2011-06-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多