Skip to content

Instantly share code, notes, and snippets.

@Axemasta
Last active December 18, 2019 11:51
Show Gist options
  • Select an option

  • Save Axemasta/939040f9c730078edfa4fafab57a73f7 to your computer and use it in GitHub Desktop.

Select an option

Save Axemasta/939040f9c730078edfa4fafab57a73f7 to your computer and use it in GitHub Desktop.
WPF File/Folder Picker
//https://user-images.githubusercontent.com/33064621/71083553-31544b00-218b-11ea-97af-74280ecaef96.png
using System;
using System.Windows;
using System.Windows.Controls;
//Requires NuGet Package: "https://www.nuget.org/packages/Microsoft.WindowsAPICodePack-Shell"
using Microsoft.WindowsAPICodePack.Dialogs;
public class FilePicker : UserControl
{
#region Properties
#region - Bindable Property Declaration
public static readonly DependencyProperty FilePathProperty =
DependencyProperty.Register(
nameof(FilePath),
typeof(string),
typeof(FilePicker),
new PropertyMetadata(
string.Empty,
OnFilePathChanged)
);
public static readonly DependencyProperty PickerTypeProperty =
DependencyProperty.Register(
nameof(PickerType),
typeof(FilePickerType),
typeof(FilePicker),
new PropertyMetadata(FilePickerType.File)
);
#endregion - Bindable Property Declaration
#region - Bindable Properties
public string FilePath
{
get { return (string)GetValue(FilePathProperty); }
set { SetValue(FilePathProperty, value); }
}
public FilePickerType PickerType
{
get { return (FilePickerType)GetValue(PickerTypeProperty); }
set { SetValue(PickerTypeProperty, value); }
}
#endregion - Bindable Properties
#region - Class Properties
public enum FilePickerType
{
File,
Folder
}
private Grid _contentGrid { get; set; }
private bool _fileDialogIsBusy = false;
private TextBox _textBox { get; set; }
#endregion - Class Properties
#endregion Properties
public FilePicker()
{
InitializeComponent();
}
#region Methods
#region - Draw View Methods
private void InitializeComponent()
{
_contentGrid = new Grid();
_contentGrid.ColumnDefinitions.Add(new ColumnDefinition() { Width = new GridLength(1, GridUnitType.Star) });
_contentGrid.ColumnDefinitions.Add(new ColumnDefinition() { Width = new GridLength(25) });
_textBox = new TextBox();
var button = new Button() { Content = "..." };
button.Click += OnButtonClicked;
_textBox.TextChanged += OnTextChanged;
Grid.SetColumn(_textBox, 0);
Grid.SetColumn(button, 1);
_contentGrid.Children.Add(_textBox);
_contentGrid.Children.Add(button);
this.Content = _contentGrid;
}
private void OnTextChanged(object sender, TextChangedEventArgs e)
{
var textbox = (TextBox)sender;
FilePath = ((TextBox)sender).Text;
}
#endregion - Draw View Methods
#region - Binded Property Changed Methods
private static void OnFilePathChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
FilePicker control = (FilePicker)d;
control._textBox.Text = (string)e.NewValue;
}
#endregion - Binded Property Changed Methods
#region - OnEvent Methods
private void OnButtonClicked(object sender, RoutedEventArgs e)
{
if (_fileDialogIsBusy)
return;
_fileDialogIsBusy = true;
string selectedPath = string.Empty;
//WIN FORMS HISSSSSSS
//using (var fbd = new System.Windows.Forms.FolderBrowserDialog())
//{
// System.Windows.Forms.DialogResult result = fbd.ShowDialog();
// if (result == System.Windows.Forms.DialogResult.OK && !string.IsNullOrWhiteSpace(fbd.SelectedPath))
// {
// selectedPath = fbd.SelectedPath;
// }
//}
if (CommonFileDialog.IsPlatformSupported)
{
var dialog = new CommonOpenFileDialog();
switch (PickerType)
{
case FilePickerType.File:
dialog.IsFolderPicker = false;
dialog.Title = "Select File";
break;
case FilePickerType.Folder:
dialog.IsFolderPicker = true;
dialog.Title = "Select Folder";
break;
}
dialog.InitialDirectory = Environment.CurrentDirectory;
CommonFileDialogResult result = dialog.ShowDialog();
if (result == CommonFileDialogResult.Ok)
{
selectedPath = dialog.FileName;
_textBox.Text = selectedPath;
}
}
else
{
MessageBox.Show("Windows Versions Below Vista Are Not Supported By This Application");
//MessageBox.Show("GET A MORE MODERN OPERATING SYSTEM!!!!");
}
System.Diagnostics.Debug.WriteLine(selectedPath);
_fileDialogIsBusy = false;
}
#endregion - OnEvent Methods
#endregion Methods
}
@Axemasta
Copy link
Author

Preview of the control!

@Axemasta
Copy link
Author

File Picker Preview - In Folder Mode

@Axemasta
Copy link
Author

A very basic file picker control, invoke in xaml as such:

<controls:FilePicker FilePath="{Binding ExportPath}" PickerType="Folder"/>

Can be easily expanded to add new bindable properties, I only needed these ones!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment