-
Notifications
You must be signed in to change notification settings - Fork 3.2k
/
Copy pathHeaderToImageConverter.cs
52 lines (42 loc) · 1.77 KB
/
HeaderToImageConverter.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
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Windows.Data;
using System.Windows.Media;
using System.Windows.Media.Imaging;
namespace WpfTreeView
{
/// <summary>
/// Converts a full path to a specific image type of a drive, folder or file.
/// </summary>
[ValueConversion(typeof(string), typeof(BitmapImage))]
public class HeaderToImageConverter : IValueConverter
{
public IDictionary<string, ImageSource> KnownFiles { get; set; }
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
// Get the full Path
var path = (string)value;
// if the path is null, ignore
if (path == null)
return null;
// Get the name of the file/folder
var name = MainWindow.GetFileFolderName(path);
// By default, we presume an image
var image = "Images/file.png";
// If the name is blank, we presume it's a drive as we cannot have a blank file or folder name
if (string.IsNullOrEmpty(name))
image = "Images/drive.png";
else if (new FileInfo(path).Attributes.HasFlag(FileAttributes.Directory)) // checks if this path is a directory
image = "Images/folder-closed.png";
else if (KnownFiles != null && KnownFiles.ContainsKey(Path.GetExtension(path)))
return KnownFiles[Path.GetExtension(path)];
return new BitmapImage(new Uri($"pack://application:,,,/{image}"));
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
}