WPF SaveFileDialog DefaultExt 被忽略?

发布于 2024-11-09 03:22:57 字数 395 浏览 7 评论 0原文

var dlg = new SaveFileDialog();
dlg.FileName = "graph";
dlg.DefaultExt = ".bmp";
dlg.Filter = "PNG|*.png|DOT|*.dot|Windows Bitmap Format|*.bmp|GIF|*.gif|JPEG|*.jpg|PDF|*.pdf|Scalable Vector Graphics|*.svg|Tag Image File Format|*.tiff";

扩展名始终默认为 .png。如果有 Filter,似乎 DefaultExt 会被忽略;那么它就默认为列表中的第一个选项。

有没有办法强制它真正尊重默认分机?

var dlg = new SaveFileDialog();
dlg.FileName = "graph";
dlg.DefaultExt = ".bmp";
dlg.Filter = "PNG|*.png|DOT|*.dot|Windows Bitmap Format|*.bmp|GIF|*.gif|JPEG|*.jpg|PDF|*.pdf|Scalable Vector Graphics|*.svg|Tag Image File Format|*.tiff";

The extension always defaults to .png. It seems the DefaultExt is ignored if there is a Filter; then it just defaults to the first option in the list.

Is there a way to force it to actually respect the default ext?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

划一舟意中人 2024-11-16 03:22:57

您应该设置 FilterIndex 属性而不是 DefaultExt。如果您仍然想使用 DefaultExt,您可以手动将其转换为正确的过滤器索引:

public static void UseDefaultExtAsFilterIndex(FileDialog dialog)
{
    var ext = "*." + dialog.DefaultExt;
    var filter = dialog.Filter;
    var filters = filter.Split('|');
    for(int i = 1; i < filters.Length; i += 2)
    {
        if(filters[i] == ext)
        {
            dialog.FilterIndex = 1 + (i - 1) / 2;
            return;
        }
    }
}

var dlg = new SaveFileDialog();
dlg.FileName = "graph";
dlg.DefaultExt = ".bmp";
dlg.Filter = "PNG|*.png|DOT|*.dot|Windows Bitmap Format|*.bmp|GIF|*.gif|JPEG|*.jpg|PDF|*.pdf|Scalable Vector Graphics|*.svg|Tag Image File Format|*.tiff";
UseDefaultExtAsFilterIndex(dlg);
dlg.ShowDialog();

You should set FilterIndex property instead of DefaultExt. If you still want to use DefaultExt, you can convert it to proper filter index manually:

public static void UseDefaultExtAsFilterIndex(FileDialog dialog)
{
    var ext = "*." + dialog.DefaultExt;
    var filter = dialog.Filter;
    var filters = filter.Split('|');
    for(int i = 1; i < filters.Length; i += 2)
    {
        if(filters[i] == ext)
        {
            dialog.FilterIndex = 1 + (i - 1) / 2;
            return;
        }
    }
}

var dlg = new SaveFileDialog();
dlg.FileName = "graph";
dlg.DefaultExt = ".bmp";
dlg.Filter = "PNG|*.png|DOT|*.dot|Windows Bitmap Format|*.bmp|GIF|*.gif|JPEG|*.jpg|PDF|*.pdf|Scalable Vector Graphics|*.svg|Tag Image File Format|*.tiff";
UseDefaultExtAsFilterIndex(dlg);
dlg.ShowDialog();
恏ㄋ傷疤忘ㄋ疼 2024-11-16 03:22:57

DefaultExt 是当用户选择没有扩展名的文件名时将使用的扩展名(至少这是我通过阅读 MSDN 的描述所理解的)。

当您的应用程序的用户
指定不带后缀的文件名
扩展名,FileDialog 附加一个
文件名的扩展名。

您可能必须将 bmp 设置为过滤器列表中的第一项。

DefaultExt is the extension that will be used if the user selects a file name with no extension (atleast that's my understanding from reading the description from MSDN).

When the user of your application
specifies a file name without an
extension, the FileDialog appends an
extension to the file name.

You may have to make bmp the first item in the filter list.

戏蝶舞 2024-11-16 03:22:57

我晚了几年,但巧合的是,我在查看此代码时找到了问题的解决方案 问题

他在那里指定了没有 . 的扩展名。然后我查看了微软文档。在示例中,还指定了 DefaultExt,但没有 .

如果使用 . 指定 DefaultExt,则 FileDialog 将自动选择过滤器的第一个扩展名。

DefaultExt 应设置为不带 ..
的扩展名
这意味着在您的示例中 dlg.DefaultExt = ".bmp"; 您需要将 ".bmp" 更改为 "bmp"...

I'm a few years too late, but coincidentally, I found a solution to the problem while looking at the code from this question.

There he specified the extension without a .. I then looked into the microsoft documentation. In the example the DefaultExt was also specified without a ..

If DefaultExt is specified with a ., FileDialog will automatically choose the first extension of the filter.

DefaultExt should be set to the extension without a ..
Meaning that in your example dlg.DefaultExt = ".bmp"; you need to change ".bmp" to "bmp"...

晨与橙与城 2024-11-16 03:22:57

根据 API 有不同的解释,但似乎工作原理相似;
当用户选择不带扩展名的文件名并且所选过滤器是通配符过滤器(例如(*.*))时,将使用DefaultExt

System.Windows.Forms .FileDialog.DefaultExt(MSDN):

当应用程序的用户指定不带扩展名的文件名时,FileDialog 会在文件名后附加扩展名。 所使用的扩展名由 Filter 和 DefaultExt 属性确定。如果在文件对话框中选择了过滤器并且该过滤器指定了扩展名,则使用该扩展名。如果所选过滤器使用通配符代替扩展名,则使用 DefaultExt 属性中指定的扩展名。

Microsoft.Win32.FileDialog.DefaultExt (MSDN):

默认情况下,AddExtension 属性尝试确定扩展名以从 Filter 属性中筛选显示的文件列表。如果无法从 Filter 属性确定扩展名,则将使用 DefaultExt。

There is different explanations depending on API, but seem to work similarly;
DefaultExt is used when the user selects a file name with no extension AND selected filter is a wildcard filter, like (*.*).

System.Windows.Forms.FileDialog.DefaultExt (MSDN):

When the user of your application specifies a file name without an extension, the FileDialog appends an extension to the file name. The extension that is used is determined by the Filter and DefaultExt properties. If a filter is selected in the FileDialog and the filter specifies an extension, then that extension is used. If the filter selected uses a wildcard in place of the extension, then the extension specified in the DefaultExt property is used.

Microsoft.Win32.FileDialog.DefaultExt (MSDN):

By default, the AddExtension property attempts to determine the extension to filter the displayed file list from the Filter property. If the extension cannot be determined from the Filter property, DefaultExt will be used instead.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文