WPF SaveFileDialog 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";
扩展名始终默认为 .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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您应该设置
FilterIndex
属性而不是DefaultExt
。如果您仍然想使用DefaultExt
,您可以手动将其转换为正确的过滤器索引:You should set
FilterIndex
property instead ofDefaultExt
. If you still want to useDefaultExt
, you can convert it to proper filter index manually:DefaultExt
是当用户选择没有扩展名的文件名时将使用的扩展名(至少这是我通过阅读 MSDN 的描述所理解的)。您可能必须将
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).You may have to make
bmp
the first item in the filter list.我晚了几年,但巧合的是,我在查看此代码时找到了问题的解决方案 问题。
他在那里指定了没有
.
的扩展名。然后我查看了微软文档。在示例中,还指定了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 theDefaultExt
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"
...根据 API 有不同的解释,但似乎工作原理相似;
当用户选择不带扩展名的文件名并且所选过滤器是通配符过滤器(例如
(*.*)
)时,将使用DefaultExt
。System.Windows.Forms .FileDialog.DefaultExt
(MSDN):Microsoft.Win32.FileDialog.DefaultExt
(MSDN):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):Microsoft.Win32.FileDialog.DefaultExt
(MSDN):