C# 如何获取PropertyGrid以显示SaveFileDialog?
我有一个属性网格控件,当用户正在将数据导出到新文件时,我希望能够在其中显示SaveFileDialog。我可以轻松地将OpenFileDialog与FileNameEditor连接起来,但似乎没有一个用于保存文件的等效类C# 如何获取PropertyGrid以显示SaveFileDialog?,c#,winforms,C#,Winforms,我有一个属性网格控件,当用户正在将数据导出到新文件时,我希望能够在其中显示SaveFileDialog。我可以轻松地将OpenFileDialog与FileNameEditor连接起来,但似乎没有一个用于保存文件的等效类 是否存在可以在System.ComponentModel.Editor属性中指定以显示SaveFileDialog的现有类?我认为没有。您必须编写从UITypeEditor派生的自己的编辑器。这应该没那么难。这很好: public class SaveFileNameEdito
是否存在可以在System.ComponentModel.Editor属性中指定以显示SaveFileDialog的现有类?我认为没有。您必须编写从UITypeEditor派生的自己的编辑器。这应该没那么难。这很好:
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;
}
}
因此,您在
propertyGrid1.SelectedObject
中设置的对象需要一个公共属性,如下所示:
private string _saveFile;
[BrowsableAttribute(true)]
[EditorAttribute(typeof(SaveFileNameEditor), typeof(System.Drawing.Design.UITypeEditor))]
public string SaveFileEditorVlad
{
get { return _saveFile; }
set { _saveFile = value; }
}
为了使的答案有效:)然后在运行时,当您编辑this属性时,省略号将显示,您将能够选择要另存为的文件