【问题标题】:How can I get a PropertyGrid to show a SaveFileDialog?如何让 PropertyGrid 显示 SaveFileDialog?
【发布时间】:2009-12-07 09:52:16
【问题描述】:

我有一个属性网格控件,我希望能够在用户正在将数据导出到新文件的过程中显示 SaveFileDialog。我可以轻松地将 OpenFileDialog 与 FileNameEditor 连接起来,但似乎没有用于保存文件的等效类。

是否有我可以在 System.ComponentModel.Editor 属性中指定的现有类以显示 SaveFileDialog?

【问题讨论】:

    标签: c# winforms


    【解决方案1】:

    这很好用:

    public class SaveFileNameEditor: UITypeEditor
    {
        public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context)
        {
            return UITypeEditorEditStyle.Modal;
        }
    
        public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value)
        {
            if (context == null || context.Instance == null || provider == null)
            {
                return base.EditValue(context, provider, value);
            }
    
            using (SaveFileDialog saveFileDialog = new SaveFileDialog())
            {
                if (value != null)
                {
                    saveFileDialog.FileName = value.ToString();
                }
    
                saveFileDialog.Title = context.PropertyDescriptor.DisplayName;
                saveFileDialog.Filter = "All files (*.*)|*.*";
                if (saveFileDialog.ShowDialog() == DialogResult.OK)
                {
                    value = saveFileDialog.FileName;
                }
            }
    
            return value;
        }
    }
    

    【讨论】:

      【解决方案2】:

      因此,您在 propertyGrid1.SelectedObject 中设置的对象需要一个公共属性,如下所示:

                  private string _saveFile;
          [BrowsableAttribute(true)]
          [EditorAttribute(typeof(SaveFileNameEditor), typeof(System.Drawing.Design.UITypeEditor))]
          public string SaveFileEditorVlad
          {
              get { return _saveFile; }
              set { _saveFile = value; }
          }
      

      为了使Stewy 的答案起作用:) 然后在运行时,当您编辑此属性时,将显示省略号,您将能够选择要另存为的文件。

      【讨论】:

        【解决方案3】:

        我认为没有。您必须编写自己的从 UITypeEditor 派生的编辑器。应该没那么难。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-09-06
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多