@@ -15,7 +15,7 @@ public partial class FilePickerTextBox : UserControl
15
15
public FilePickerTextBox ( )
16
16
{
17
17
ClearFileCommand = new RelayCommand ( ClearFile , CanExecuteClearFile ) ;
18
- OpenFileDialogCommand = new RelayCommand ( OpenFilePicker , CanExecuteOpenFilePicker ) ;
18
+ OpenFileDialogCommand = new RelayCommand ( OpenPicker , CanExecuteOpenPicker ) ;
19
19
InitializeComponent ( ) ;
20
20
}
21
21
@@ -37,6 +37,9 @@ public FilePickerTextBox()
37
37
public static readonly DependencyProperty IsRequiredProperty =
38
38
DependencyProperty . Register ( "IsRequired" , typeof ( bool ) , typeof ( FilePickerTextBox ) ) ;
39
39
40
+ public static readonly DependencyProperty IsFolderPickerProperty =
41
+ DependencyProperty . Register ( "IsFolderPicker" , typeof ( bool ) , typeof ( FilePickerTextBox ) ) ;
42
+
40
43
41
44
/// <summary>
42
45
/// Gets or sets the clear file command.
@@ -115,6 +118,46 @@ public bool IsRequired
115
118
}
116
119
117
120
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
+
118
161
/// <summary>
119
162
/// Opens the file picker.
120
163
/// </summary>
@@ -135,14 +178,19 @@ private void OpenFilePicker()
135
178
136
179
137
180
/// <summary>
138
- /// Determines whether this instance can execute OpenFilePicker .
181
+ /// Opens the folder picker .
139
182
/// </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 ( )
144
184
{
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 ;
146
194
}
147
195
148
196
@@ -174,6 +222,7 @@ private bool CanExecuteClearFile()
174
222
/// <seealso cref="System.Windows.Controls.ValidationRule" />
175
223
public class FileExistsValidationRule : ValidationRule
176
224
{
225
+ public bool IsFolder { get ; set ; }
177
226
public bool IsRequired { get ; set ; }
178
227
179
228
public override ValidationResult Validate ( object value , CultureInfo cultureInfo )
@@ -182,9 +231,12 @@ public override ValidationResult Validate(object value, CultureInfo cultureInfo)
182
231
if ( ! IsRequired && string . IsNullOrEmpty ( filename ) )
183
232
return ValidationResult . ValidResult ;
184
233
185
- if ( ! File . Exists ( filename ) )
234
+ if ( ! IsFolder && ! File . Exists ( filename ) )
186
235
return new ValidationResult ( false , $ "File does not exist") ;
187
236
237
+ if ( IsFolder && ! Directory . Exists ( filename ) )
238
+ return new ValidationResult ( false , $ "Directory does not exist") ;
239
+
188
240
return ValidationResult . ValidResult ;
189
241
}
190
242
}
0 commit comments