-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
StringToSolidBrushConverter.cs
46 lines (42 loc) · 1.38 KB
/
StringToSolidBrushConverter.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
using System;
using System.Globalization;
using System.Windows.Data;
using System.Windows.Markup;
using System.Windows.Media;
namespace stringtobrushconverter
{
public class StringToSolidBrushConverter : MarkupExtension, IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value == null)
{
throw new ArgumentException("ColorToBrushConverter : Value can not be null");
}
try
{
var colorFromString = ColorConverter.ConvertFromString(value.ToString());
if (colorFromString != null)
{
if (colorFromString is Color)
{
return new SolidColorBrush(((Color)colorFromString));
}
}
}
catch (Exception e)
{
return new SolidColorBrush(Colors.Transparent);
}
return new SolidColorBrush(Colors.Transparent);
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
public override object ProvideValue(IServiceProvider serviceProvider)
{
return this;
}
}
}