using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; using GalaSoft.MvvmLight.Command; namespace DriveDotNetGuiClient.Controls { public enum BrowseType { SelectFolder, SelectFile }; /// /// Interaction logic for PathAndBrowse.xaml /// public partial class PathAndBrowse : UserControl { public PathAndBrowse() { InitializeComponent(); cmdBrowse = new RelayCommand(OnBrowse); } public RelayCommand cmdBrowse { get; private set; } // Dependency Property public static readonly DependencyProperty SelectedPathProperty = DependencyProperty.Register("SelectedPath", typeof(string), typeof(PathAndBrowse), new FrameworkPropertyMetadata(string.Empty) { BindsTwoWayByDefault=true }); public static readonly DependencyProperty FileExtensionProperty = DependencyProperty.Register("FileExtension", typeof(string), typeof(PathAndBrowse), new FrameworkPropertyMetadata("txt files (*.txt)|*.txt|All files (*.*)|*.*")); public static readonly DependencyProperty BrowseTypeProperty = DependencyProperty.Register("BrowseType", typeof(BrowseType), typeof(PathAndBrowse), new FrameworkPropertyMetadata(BrowseType.SelectFile)); // .NET Property wrapper public string SelectedPath { get { return (string)GetValue(SelectedPathProperty); } set { SetValue(SelectedPathProperty, value); } } public string FileExtension { get { return (string)GetValue(FileExtensionProperty); } set { SetValue(FileExtensionProperty, value); } } public BrowseType BrowseType { get { return (BrowseType)GetValue(BrowseTypeProperty); } set { SetValue(BrowseTypeProperty, value); } } private void OnBrowse() { if (BrowseType == Controls.BrowseType.SelectFile) { System.Windows.Forms.OpenFileDialog fileDialog = new System.Windows.Forms.OpenFileDialog(); fileDialog.Filter = FileExtension; if (fileDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK) { SelectedPath = fileDialog.FileName; } } else if (BrowseType == Controls.BrowseType.SelectFolder) { System.Windows.Forms.FolderBrowserDialog dlg = new System.Windows.Forms.FolderBrowserDialog(); dlg.SelectedPath = SelectedPath; if (dlg.ShowDialog() == System.Windows.Forms.DialogResult.OK) { SelectedPath = dlg.SelectedPath; } } } } }