-
Notifications
You must be signed in to change notification settings - Fork 5
/
HeaderFilterConverter.cs
89 lines (78 loc) · 2.98 KB
/
HeaderFilterConverter.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
using System;
using System.Windows.Data;
using System.Windows;
using System.ComponentModel;
using System.Windows.Controls;
using System.Text;
using System.IO;
namespace FancyGrid
{
/// <summary>
/// This converter will:
/// - Take the header
/// - Take the filtered word (if any)
/// - Add '(Filter: (bold)x(/bold))' to the header
/// </summary>
public class HeaderFilterConverter : IMultiValueConverter
{
/// <summary>
/// Create a nice looking header
/// </summary>
/// <param name="values"></param>
/// <param name="targetType"></param>
/// <param name="parameter"></param>
/// <param name="culture"></param>
/// <returns></returns>
public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
// Get values
string filter = values[0] as string;
string headerText = values[1] as string;
string filtertype = "";
if (filter.StartsWith("<"))
filtertype = "Less Than";
else if (filter.StartsWith(">"))
filtertype = "Greater Than";
else if (filter.StartsWith("="))
filtertype = "Exactly";
else if (filter.StartsWith("!"))
filtertype = "Not";
else if (filter.StartsWith("~"))
filtertype = "Doesn't Contain";
else if (filter.StartsWith(@""""))
filtertype = "Blank";
else if (filter.Equals("*"))
filtertype = "Any";
else
filtertype = "Contains";
// Generate header text
string text = "{0}{3}" + headerText + " {4}";
if (!String.IsNullOrEmpty(filter))
text += "({2}" + filtertype + "{4})";
text += "{1}";
// Escape special XML characters like <>&'
text = new System.Xml.Linq.XText(text).ToString();
// Format the text
text = String.Format(text,
@"<TextBlock xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation'>",
"</TextBlock>", "<Run FontWeight='bold' Text='", "<Run Text='", @"'/>");
// Convert to stream
MemoryStream stream = new MemoryStream(ASCIIEncoding.UTF8.GetBytes(text));
// Convert to object
TextBlock block = (TextBlock)System.Windows.Markup.XamlReader.Load(stream);
return block;
}
/// <summary>
/// Not required
/// </summary>
/// <param name="value"></param>
/// <param name="targetTypes"></param>
/// <param name="parameter"></param>
/// <param name="culture"></param>
/// <returns></returns>
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}
}