-
Notifications
You must be signed in to change notification settings - Fork 0
/
FileExplorer.xaml.cs
97 lines (90 loc) · 2.87 KB
/
FileExplorer.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
using System;
using System.Windows;
using System.Windows.Controls;
using System.IO;
using System.Windows.Data;
using System.Windows.Media.Imaging;
using System.Globalization;
namespace LungMorphApp
{
/// <summary>
/// Interaction logic for FileExplorer.xaml
/// </summary>
public partial class FileExplorer : UserControl
{
object dummyNode = null; // Tree View File Explorer
string _SelectedPath = "";
public string SelectedPath { get { return _SelectedPath; } }
public FileExplorer()
{
InitializeComponent();
}
void Initialization(object sender, RoutedEventArgs e)
{
foreach (string s in Directory.GetLogicalDrives()) { // Tree View
TreeViewItem item = new TreeViewItem();
item.Header=s;
item.Tag=s;
item.FontWeight=FontWeights.Normal;
item.Items.Add(dummyNode);
item.Expanded+=new RoutedEventHandler(Folder_Expanded);
foldersItem.Items.Add(item);
}
}
void Folder_Expanded(object sender, RoutedEventArgs e)
{
TreeViewItem item = (TreeViewItem)sender;
if (item.Items.Count==1&&item.Items[0]==dummyNode) {
item.Items.Clear();
try {
foreach (string s in Directory.GetDirectories(item.Tag.ToString())) {
TreeViewItem subitem = new TreeViewItem();
subitem.Header=s.Substring(s.LastIndexOf("\\")+1);
subitem.Tag=s;
subitem.FontWeight=FontWeights.Normal;
subitem.Items.Add(dummyNode);
subitem.Expanded+=new RoutedEventHandler(Folder_Expanded);
item.Items.Add(subitem);
}
} catch { throw new Exception("Can not expand the selected folder."); }
}
}
void FoldersItem_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
{
TreeView tree = (TreeView)sender;
TreeViewItem temp = ((TreeViewItem)tree.SelectedItem);
if (temp==null) return;
_SelectedPath="";
string temp1 = ""; string temp2 = "";
while (true) {
temp1=temp.Header.ToString();
if (temp1.Contains(@"\")) temp2="";
_SelectedPath=temp1+temp2+_SelectedPath;
if (temp.Parent.GetType().Equals(typeof(TreeView))) break;
temp=((TreeViewItem)temp.Parent);
temp2=@"\";
}
}
}
[ValueConversion(typeof(string), typeof(bool))]
public class HeaderToImageConverter : IValueConverter
{
public static HeaderToImageConverter Instance = new HeaderToImageConverter();
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if ((value as string).Contains(@"\")) {
Uri uri = new Uri("pack://application:,,,/Resources/diskdrive.png");
BitmapImage source = new BitmapImage(uri);
return source;
} else {
Uri uri = new Uri("pack://application:,,,/Resources/folder.png");
BitmapImage source = new BitmapImage(uri);
return source;
}
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotSupportedException("Cannot convert back");
}
}
}