开发者

How can I get a PropertyGrid to show a SaveFileDialog?

I have a property grid control where I want to be able to display a SaveFileDialog as the user is in the process of exporting data to a new file. I can easily hook up an OpenFileDialog with a FileNameEditor but there doesn't seem to be an equivalent class for saving files.

Is there an existing class that I can specify in the System.Comp开发者_如何学GoonentModel.Editor attribute so that a SaveFileDialog is displayed?


This works fine:

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


So the object that you set in the propertyGrid1.SelectedObject needs a public property like the following:

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

in order to make Stewy's answer work :) Then at runtime, when you edit the this property, the ellipsis will show and you'll be able to select a file to Save As.


I don't think there is. You will have to write your own editor derived from UITypeEditor. It should not be that difficult.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜