Show something other than, "All files" in a FileOpenPicker that isn't filtered to show all files

489 views Asked by At

I'm displaying an open file dialog via Windows::Storage::Pickers::FileOpenPicker in a winrt project I'm writing. When I set the FileTypeFilter property on the picker it works, but the name shown still says, "All files."

I've seen on learn.microsoft.com that FileSavePicker has a FileTypeChoices property that holds a map instead of a vector allowing the save picker to have names for each type, but I only can find the FileTypeFilter vector for the file open picker. Even the screenshots for Microsoft's examples say "All files" even though it's filtered

I know there has to be some way to do it, because I've seen plenty of programs with file open pickers that are able to display names.


Does anyone know how to get the open file picker to show something other than the default "All files"?

NOTE: I've been doing c++ for a few years, but I am still very new to c++/winrt (as in like, just learned what winrt was earlier this week) so I'm still not sure how to do a lot of basic things, like set up a file picker the right way

This is the code that creates and opens the dialog:

// Andrew Pratt 2021
// MainPage.cpp

#include "pch.h"
#include "MainPage.h"
#include "MainPage.g.cpp"

#include "winrt/Windows.Storage.h"
#include "winrt/Windows.Storage.Pickers.h"
#include "winrt/Windows.Storage.Pickers.Provider.h"

using namespace winrt;
using namespace Windows::Foundation;
using namespace Windows::Foundation::Collections;
using namespace Windows::UI::Xaml;
using namespace Windows::UI::Popups;
using namespace Windows::UI::Xaml::Controls;
using namespace Windows::Storage;
using namespace Windows::Storage::Pickers;

namespace winrt::AltBrickUi::implementation
{
    MainPage::MainPage()
    {
        InitializeComponent();
    }
}


winrt::fire_and_forget winrt::AltBrickUi::implementation::MainPage::easyAlert(const IInspectable& title, const IInspectable& msg, const winrt::hstring& closeText)
{
    ContentDialog alert{ ContentDialog() };
    alert.Title(title);
    alert.Content(msg);
    alert.CloseButtonText(closeText);
    alert.ShowAsync();
    
    co_return;
}


void winrt::AltBrickUi::implementation::MainPage::MenuFlyoutItem_Click(winrt::Windows::Foundation::IInspectable const& sender, winrt::Windows::UI::Xaml::RoutedEventArgs const& e)
{
    showJsonFileOpenPicker();
}


winrt::fire_and_forget winrt::AltBrickUi::implementation::MainPage::showJsonFileOpenPicker()
{
    // Create file picker
    Pickers::FileOpenPicker picker{};
    picker.ViewMode(Pickers::PickerViewMode::List);
    picker.FileTypeFilter().ReplaceAll({ winrt::to_hstring(L".json") });
    // Open dialogue and get picked file
    StorageFile pickedFile = co_await picker.PickSingleFileAsync();

    if (pickedFile)
        easyAlert(winrt::box_value(L"File picked!"), winrt::box_value(pickedFile.DisplayName()), L"Cool");
    else
        easyAlert(winrt::box_value(L"Nothing Picked"), NULL, L"Alright");

    co_return;
}

Here's a snippet of what the file picker looks like when I run it:enter image description here

An example of a file picker that displays the way I need it to (this one is from Notepad): enter image description here

1

There are 1 answers

1
dear_vv On

The situation you mentioned always happens in desktop application, such as wpf app. please read here to know more.

I have to say that uwp doesn’t provide such api to do this. If you do want this feature, please submit your feature requirement with Windows Feedback Hub app.