blob: f8c77cefdb206895d00f8e85a950ab8c03770b84 (
plain)
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
|
using System;
using System.Linq;
using System.Reflection;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
namespace Xamarin.Forms.Platform.WinPhone
{
internal static class FrameworkElementExtensions
{
static readonly Lazy<ConcurrentDictionary<Type, DependencyProperty>> ForegroundProperties =
new Lazy<ConcurrentDictionary<Type, DependencyProperty>>(() => new ConcurrentDictionary<Type, DependencyProperty>());
public static Brush GetForeground(this FrameworkElement element)
{
if (element == null)
throw new ArgumentNullException("element");
return (Brush)element.GetValue(GetForegroundProperty(element));
}
public static System.Windows.Data.Binding GetForegroundBinding(this FrameworkElement element)
{
System.Windows.Data.BindingExpression expr = element.GetBindingExpression(GetForegroundProperty(element));
if (expr == null)
return null;
return expr.ParentBinding;
}
public static void SetForeground(this FrameworkElement element, Brush foregroundBrush)
{
if (element == null)
throw new ArgumentNullException("element");
element.SetValue(GetForegroundProperty(element), foregroundBrush);
}
public static void SetForeground(this FrameworkElement element, System.Windows.Data.Binding binding)
{
if (element == null)
throw new ArgumentNullException("element");
element.SetBinding(GetForegroundProperty(element), binding);
}
static DependencyProperty GetForegroundProperty(FrameworkElement element)
{
if (element is Control)
return Control.ForegroundProperty;
if (element is TextBlock)
return TextBlock.ForegroundProperty;
Type type = element.GetType();
DependencyProperty foregroundProperty;
if (!ForegroundProperties.Value.TryGetValue(type, out foregroundProperty))
{
FieldInfo field = type.GetFields(BindingFlags.Public | BindingFlags.Static).FirstOrDefault(f => f.Name == "ForegroundProperty");
if (field == null)
throw new ArgumentException("type is not a Foregroundable type");
var property = (DependencyProperty)field.GetValue(null);
ForegroundProperties.Value.TryAdd(type, property);
return property;
}
return foregroundProperty;
}
}
}
|