RadFilePathPicker throw error and clear if extension is wrong

0 Answers 21 Views
FilePathPicker
Ict
Top achievements
Rank 1
Iron
Ict asked on 27 Oct 2023, 09:03 AM | edited on 27 Oct 2023, 09:58 AM

I'm making a simple file upload application that should only accept Excel files.
I've found out how I need to set the filter on the RadOpenFileDialog and I'm throwing an error (via messagebox) when the extension is wrong when dropping a file on the control.

        public RelayCommand FilePathChangedCommand => new RelayCommand((ea) =>
        {
            if (!string.IsNullOrEmpty(FilePath))
            {
                var extension = Path.GetExtension(FilePath);
                if (!extension.In("xls", "xlsx", "xlsm"))
                {
                    //throw error
                }
                else
                {
                    //process file
                }
            }
        });

When the extension is wrong, I would also like to clear the value in the RadFilePathPicker. But when I make it empty in the FilePathChanged event, it doesn't do anything.

What would be the correct approach to clear the control in this case?

Dinko
Telerik team
commented on 01 Nov 2023, 05:46 AM

Hello Ict,

To clear the control, you could update the FilePath property to an empty string. However, directly setting it inside the FilePathChanged event might not work as expected. Instead, you can try using the Dispatcher to set the FilePath property. Here is an updated version of your code:

public RelayCommand FilePathChangedCommand => new RelayCommand((ea) =>
{
    if (!string.IsNullOrEmpty(FilePath))
    {
        var extension = Path.GetExtension(FilePath);
        if (!extension.In("xls", "xlsx", "xlsm"))
        {
            // Show error message
            MessageBox.Show("Invalid file extension. Please select a valid Excel file.");
            
            // Clear FilePath asynchronously on the UI thread
            Application.Current.Dispatcher.Invoke(() =>
            {
                FilePath = string.Empty;
            });
        }
        else
        {
            // Process the file
        }
    }
});

Let me know how this work on your side.

No answers yet. Maybe you can help?

Tags
FilePathPicker
Asked by
Ict
Top achievements
Rank 1
Iron
Share this question
or