@@ -15,7 +15,7 @@ public partial class FilePickerTextBox : UserControl
1515 public FilePickerTextBox ( )
1616 {
1717 ClearFileCommand = new RelayCommand ( ClearFile , CanExecuteClearFile ) ;
18- OpenFileDialogCommand = new RelayCommand ( OpenFilePicker , CanExecuteOpenFilePicker ) ;
18+ OpenFileDialogCommand = new RelayCommand ( OpenPicker , CanExecuteOpenPicker ) ;
1919 InitializeComponent ( ) ;
2020 }
2121
@@ -37,6 +37,9 @@ public FilePickerTextBox()
3737 public static readonly DependencyProperty IsRequiredProperty =
3838 DependencyProperty . Register ( "IsRequired" , typeof ( bool ) , typeof ( FilePickerTextBox ) ) ;
3939
40+ public static readonly DependencyProperty IsFolderPickerProperty =
41+ DependencyProperty . Register ( "IsFolderPicker" , typeof ( bool ) , typeof ( FilePickerTextBox ) ) ;
42+
4043
4144 /// <summary>
4245 /// Gets or sets the clear file command.
@@ -115,6 +118,46 @@ public bool IsRequired
115118 }
116119
117120
121+ /// <summary>
122+ /// Gets or sets a value indicating whether this instance is folder picker.
123+ /// </summary>
124+ /// <value>
125+ /// <c>true</c> if this instance is folder picker; otherwise, <c>false</c>.
126+ /// </value>
127+ public bool IsFolderPicker
128+ {
129+ get { return ( bool ) GetValue ( IsFolderPickerProperty ) ; }
130+ set { SetValue ( IsFolderPickerProperty , value ) ; }
131+ }
132+
133+
134+ /// <summary>
135+ /// Opens the picker.
136+ /// </summary>
137+ private void OpenPicker ( )
138+ {
139+ if ( IsFolderPicker )
140+ {
141+ OpenFolderPicker ( ) ;
142+ return ;
143+ }
144+
145+ OpenFilePicker ( ) ;
146+ }
147+
148+
149+ /// <summary>
150+ /// Determines whether this instance can execute OpenPicker.
151+ /// </summary>
152+ /// <returns>
153+ /// <c>true</c> if this instance can execute OpenPicker; otherwise, <c>false</c>.
154+ /// </returns>
155+ private bool CanExecuteOpenPicker ( )
156+ {
157+ return true ;
158+ }
159+
160+
118161 /// <summary>
119162 /// Opens the file picker.
120163 /// </summary>
@@ -135,14 +178,19 @@ private void OpenFilePicker()
135178
136179
137180 /// <summary>
138- /// Determines whether this instance can execute OpenFilePicker .
181+ /// Opens the folder picker .
139182 /// </summary>
140- /// <returns>
141- /// <c>true</c> if this instance can execute OpenFilePicker; otherwise, <c>false</c>.
142- /// </returns>
143- private bool CanExecuteOpenFilePicker ( )
183+ private void OpenFolderPicker ( )
144184 {
145- return true ;
185+ var folderBrowserDialog = new System . Windows . Forms . FolderBrowserDialog
186+ {
187+ Description = Title ,
188+ InitialDirectory = InitialDirectory ,
189+ UseDescriptionForTitle = true
190+ } ;
191+ var dialogResult = folderBrowserDialog . ShowDialog ( ) ;
192+ if ( dialogResult == System . Windows . Forms . DialogResult . OK )
193+ FileName = folderBrowserDialog . SelectedPath ;
146194 }
147195
148196
@@ -174,6 +222,7 @@ private bool CanExecuteClearFile()
174222 /// <seealso cref="System.Windows.Controls.ValidationRule" />
175223 public class FileExistsValidationRule : ValidationRule
176224 {
225+ public bool IsFolder { get ; set ; }
177226 public bool IsRequired { get ; set ; }
178227
179228 public override ValidationResult Validate ( object value , CultureInfo cultureInfo )
@@ -182,9 +231,12 @@ public override ValidationResult Validate(object value, CultureInfo cultureInfo)
182231 if ( ! IsRequired && string . IsNullOrEmpty ( filename ) )
183232 return ValidationResult . ValidResult ;
184233
185- if ( ! File . Exists ( filename ) )
234+ if ( ! IsFolder && ! File . Exists ( filename ) )
186235 return new ValidationResult ( false , $ "File does not exist") ;
187236
237+ if ( IsFolder && ! Directory . Exists ( filename ) )
238+ return new ValidationResult ( false , $ "Directory does not exist") ;
239+
188240 return ValidationResult . ValidResult ;
189241 }
190242 }
0 commit comments