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?
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.