-
Notifications
You must be signed in to change notification settings - Fork 10
/
FileSelector.xaml.cs
316 lines (251 loc) · 11.2 KB
/
FileSelector.xaml.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
using System;
using System.Windows;
using System.Windows.Controls;
using Microsoft.Win32;
namespace CrmCodeGenerator.Controls
{
/// <summary>
/// A simple file selector which supports save-as and
/// open-file dialogs.
/// </summary>
public partial class FileSelector : UserControl
{
#region Mode dependency property
/// <summary>
/// Defines whether a file needs to be opened or saved.
/// </summary>
public static readonly DependencyProperty ModeProperty;
/// <summary>
/// A property wrapper for the <see cref="ModeProperty"/>
/// dependency property:<br/>
/// Defines whether a file needs to be opened or saved.
/// </summary>
public FileSelectorMode Mode
{
get { return (FileSelectorMode)GetValue(ModeProperty); }
set { SetValue(ModeProperty, value); }
}
/// <summary>
/// Handles changes on the <see cref="ModeProperty"/> dependency property. As
/// WPF internally uses the dependency property system and bypasses the
/// <see cref="Mode"/> property wrapper, updates should be handled here.
/// </summary>
/// <param name="d">The currently processed owner of the property.</param>
/// <param name="e">Provides information about the updated property.</param>
private static void ModePropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
}
#endregion
#region FileFilter dependency property
/// <summary>
/// Defines a file filter for the file selection dialog.
/// </summary>
public static readonly DependencyProperty FileFilterProperty;
/// <summary>
/// A property wrapper for the <see cref="FileFilterProperty"/>
/// dependency property:<br/>
/// Defines a file filter for the file selection dialog.
/// </summary>
public string FileFilter
{
get { return (string)GetValue(FileFilterProperty); }
set { SetValue(FileFilterProperty, value); }
}
/// <summary>
/// Handles changes on the <see cref="FileFilterProperty"/> dependency property. As
/// WPF internally uses the dependency property system and bypasses the
/// <see cref="FileFilter"/> property wrapper, updates should be handled here.
/// </summary>
/// <param name="d">The currently processed owner of the property.</param>
/// <param name="e">Provides information about the updated property.</param>
private static void FileFilterPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
}
#endregion
#region FileName dependency property
/// <summary>
/// Gets or sets the file path to be displayed.
/// </summary>
public static readonly DependencyProperty FileNameProperty;
/// <summary>
/// A property wrapper for the <see cref="FileNameProperty"/>
/// dependency property:<br/>
/// Gets or sets the file path to be displayed.
/// </summary>
public string FileName
{
get { return (string)GetValue(FileNameProperty); }
set { SetValue(FileNameProperty, value); }
}
/// <summary>
/// Handles changes on the <see cref="FileNameProperty"/> dependency property. As
/// WPF internally uses the dependency property system and bypasses the
/// <see cref="FileName"/> property wrapper, updates should be handled here.
/// </summary>
/// <param name="d">The currently processed owner of the property.</param>
/// <param name="e">Provides information about the updated property.</param>
private static void FileNamePropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
FileSelector owner = (FileSelector)d;
owner.UpdateFileNameDisplay();
}
#endregion
#region Folder dependency property
/// <summary>
/// Gets or sets the default Folder.
/// </summary>
public static readonly DependencyProperty FolderProperty;
/// <summary>
/// A property wrapper for the <see cref="FolderProperty"/>
/// dependency property:<br/>
/// Gets or sets the Folder.
/// </summary>
public string Folder
{
get { return (string)GetValue(FolderProperty); }
set { SetValue(FolderProperty, value); }
}
/// <summary>
/// Handles changes on the <see cref="FolderProperty"/> dependency property. As
/// WPF internally uses the dependency property system and bypasses the
/// <see cref="Folder"/> property wrapper, updates should be handled here.
/// </summary>
/// <param name="d">The currently processed owner of the property.</param>
/// <param name="e">Provides information about the updated property.</param>
private static void FolderPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
}
#endregion
#region MaxDisplayLength dependency property
/// <summary>
/// The maximum number of characters of the file path to be displayed.
/// If the path is longer than this value, it will be shortened.
/// Set to 0 in order to always show the full path.
/// </summary>
public static readonly DependencyProperty MaxDisplayLengthProperty;
/// <summary>
/// A property wrapper for the <see cref="MaxDisplayLengthProperty"/>
/// dependency property:<br/>
/// The maximum number of characters of the file path to be displayed.
/// If the path is longer than this value, it will be shortened.
/// Set to 0 in order to always show the full path.
/// </summary>
public int MaxDisplayLength
{
get { return (int)GetValue(MaxDisplayLengthProperty); }
set { SetValue(MaxDisplayLengthProperty, value); }
}
/// <summary>
/// Handles changes on the <see cref="MaxDisplayLengthProperty"/> dependency property. As
/// WPF internally uses the dependency property system and bypasses the
/// <see cref="MaxDisplayLength"/> property wrapper, updates should be handled here.
/// </summary>
/// <param name="d">The currently processed owner of the property.</param>
/// <param name="e">Provides information about the updated property.</param>
private static void MaxDisplayLengthPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
FileSelector owner = (FileSelector)d;
owner.UpdateFileNameDisplay();
}
#endregion
#region construction
/// <summary>
/// Inits the control's dependency properties.
/// </summary>
static FileSelector()
{
//register dependency properties
FrameworkPropertyMetadata md = new FrameworkPropertyMetadata(0, MaxDisplayLengthPropertyChanged);
MaxDisplayLengthProperty =
DependencyProperty.Register("MaxDisplayLength", typeof(int), typeof(FileSelector), md);
md = new FrameworkPropertyMetadata("", FileNamePropertyChanged);
FileNameProperty = DependencyProperty.Register("FileName", typeof(string), typeof(FileSelector), md);
md = new FrameworkPropertyMetadata(FileSelectorMode.Open, ModePropertyChanged);
ModeProperty = DependencyProperty.Register("Mode", typeof(FileSelectorMode), typeof(FileSelector), md);
md = new FrameworkPropertyMetadata(null, FileFilterPropertyChanged);
FileFilterProperty = DependencyProperty.Register("FileFilter", typeof(string), typeof(FileSelector), md);
md = new FrameworkPropertyMetadata(null, FolderPropertyChanged);
FolderProperty = DependencyProperty.Register("Folder", typeof(string), typeof(FileSelector), md);
}
public FileSelector()
{
InitializeComponent();
}
#endregion
#region update displayed file name
/// <summary>
/// Updates the displayed control according to the
/// <see cref="FileName"/> and <see cref="MaxDisplayLength"/>
/// properties.
/// </summary>
private void UpdateFileNameDisplay()
{
string fileName = FileName;
if (String.IsNullOrEmpty(fileName))
{
txtFileName.Text = String.Empty;
return;
}
int length = fileName.Length;
int maxLength = MaxDisplayLength;
if (maxLength > 0 && length > maxLength + 1)
{
fileName = "~" + fileName.Substring(length - maxLength, maxLength);
}
txtFileName.Text = fileName;
}
#endregion
#region browse file
/// <summary>
/// Displays a file dialog and assign the selected
/// file to the <see cref="FileName"/> property.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnBrowse_Click(object sender, RoutedEventArgs e)
{
FileDialog dlg;
if (Mode == FileSelectorMode.Open)
dlg = new OpenFileDialog();
else
dlg = new SaveFileDialog();
dlg.Filter = FileFilter;
var fullpath = System.IO.Path.Combine(Folder, FileName);
dlg.InitialDirectory = System.IO.Path.GetDirectoryName(System.IO.Path.GetFullPath(fullpath));
dlg.FileName = System.IO.Path.GetFileName(FileName);
bool? result = dlg.ShowDialog();
if (result == true)
{
FileName = dlg.FileName;
var selectedPath = System.IO.Path.GetDirectoryName(dlg.FileName);
var selectedFile = System.IO.Path.GetFileName(dlg.FileName);
var relativeFileName = MakeRelative(selectedPath, Folder);
FileName = System.IO.Path.Combine(relativeFileName, selectedFile);
}
}
#endregion
static string MakeRelative(string fromAbsolutePath, string toDirectory)
{
if (!System.IO.Path.IsPathRooted(fromAbsolutePath))
return fromAbsolutePath; // we can't make a relative if it's not rooted(C:\) so we'll assume we already have a relative path.
if (!toDirectory[toDirectory.Length - 1].Equals("\\"))
toDirectory += "\\";
if (!fromAbsolutePath[fromAbsolutePath.Length - 1].Equals("\\"))
fromAbsolutePath += "\\";
System.Uri from = new Uri(fromAbsolutePath);
System.Uri to = new Uri(toDirectory);
Uri relativeUri = to.MakeRelativeUri(from);
Uri t2 = from.MakeRelativeUri(to);
return relativeUri.ToString();
}
}
/// <summary>
/// Defines whether the selector displays a file-open or
/// file-save dialog.
/// </summary>
public enum FileSelectorMode
{
Open = 0,
Save = 1
}
}