[NUI] Use 'Container GetParent() for derived class' instead of Parent.
[platform/core/csapi/tizenfx.git] / src / Tizen.NUI / src / internal / XamlBinding / DataTemplate.cs
1 using System;
2 using System.Collections.Generic;
3
4 namespace Tizen.NUI.Binding
5 {
6     internal class DataTemplate : ElementTemplate
7     {
8         public DataTemplate()
9         {
10         }
11
12         public DataTemplate(Type type) : base(type)
13         {
14         }
15
16         public DataTemplate(Func<object> loadTemplate) : base(loadTemplate)
17         {
18         }
19
20         public IDictionary<BindableProperty, BindingBase> Bindings { get; } = new Dictionary<BindableProperty, BindingBase>();
21
22         public IDictionary<BindableProperty, object> Values { get; } = new Dictionary<BindableProperty, object>();
23
24         public void SetBinding(BindableProperty property, BindingBase binding)
25         {
26             if (property == null)
27                 throw new ArgumentNullException(nameof(property));
28             if (binding == null)
29                 throw new ArgumentNullException(nameof(binding));
30
31             Values.Remove(property);
32             Bindings[property] = binding;
33         }
34
35         public void SetValue(BindableProperty property, object value)
36         {
37             if (property == null)
38                 throw new ArgumentNullException(nameof(property));
39
40             Bindings.Remove(property);
41             Values[property] = value;
42         }
43
44         internal override void SetupContent(object item)
45         {
46             ApplyBindings(item);
47             ApplyValues(item);
48         }
49
50         void ApplyBindings(object item)
51         {
52             if (Bindings == null)
53                 return;
54
55             var bindable = item as BindableObject;
56             if (bindable == null)
57                 return;
58
59             foreach (KeyValuePair<BindableProperty, BindingBase> kvp in Bindings)
60             {
61                 if (Values.ContainsKey(kvp.Key))
62                     throw new InvalidOperationException("Binding and Value found for " + kvp.Key.PropertyName);
63
64                 bindable.SetBinding(kvp.Key, kvp.Value.Clone());
65             }
66         }
67
68         void ApplyValues(object item)
69         {
70             if (Values == null)
71                 return;
72
73             var bindable = item as BindableObject;
74             if (bindable == null)
75                 return;
76             foreach (KeyValuePair<BindableProperty, object> kvp in Values)
77                 bindable.SetValue(kvp.Key, kvp.Value);
78         }
79     }
80 }