-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathTemplateBindingExpression.cs
87 lines (73 loc) · 2.84 KB
/
TemplateBindingExpression.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
using Appercode.UI.Controls;
using Appercode.UI.Internals;
using Appercode.UI.Markup;
using System;
using System.Windows;
namespace Appercode.UI
{
public class TemplateBindingExpression : Expression
{
internal DependencyObject Source;
private DependencyProperty sourceProperty;
private UIElement target;
private DependencyProperty targetProperty;
private DependencyPropertyChangedWeakListener listener;
private bool runtimeCheck;
private TemplateBindingExtension bindingExtension;
internal TemplateBindingExpression()
{
}
internal TemplateBindingExpression(DependencyObject source, TemplateBindingExtension binding, bool runtimeCheck)
{
this.Source = source;
this.sourceProperty = binding.Property;
this.bindingExtension = binding;
this.runtimeCheck = runtimeCheck;
}
internal static bool PropertyTypesCompatible(Type sourceType, Type targetType, out bool runtimeCheck)
{
if (sourceType == targetType || targetType.IsAssignableFrom(sourceType))
{
runtimeCheck = false;
return true;
}
if (sourceType.IsAssignableFrom(targetType))
{
runtimeCheck = true;
return true;
}
runtimeCheck = false;
return false;
}
internal override object GetValue(DependencyObject d, DependencyProperty dp)
{
object value = this.Source.GetValue(this.sourceProperty);
if (this.bindingExtension.Converter != null)
{
value = this.bindingExtension.Converter.Convert(value, this.targetProperty.PropertyType, this.bindingExtension.ConverterParameter, System.Threading.Thread.CurrentThread.CurrentCulture);
}
if (!this.runtimeCheck || this.IsValidValueForUpdate(value, this.targetProperty.PropertyType))
{
return value;
}
return this.targetProperty.GetDefaultValue(this.target);
}
internal override void OnAttach(DependencyObject d, DependencyProperty dp)
{
this.target = d as UIElement;
this.targetProperty = dp;
this.listener = new DependencyPropertyChangedWeakListener(this.Source, this);
}
internal override void OnDetach(DependencyObject d, DependencyProperty dp)
{
this.listener.Disconnect();
}
internal void SourcePropertyChanged(object sender, DependencyProperty dp)
{
if (dp == this.sourceProperty)
{
this.target.RefreshExpression(this.targetProperty);
}
}
}
}