[NUI] Introduce CollectionView and related classes. (#2525)
[platform/core/csapi/tizenfx.git] / src / Tizen.NUI / src / public / Template / DataTemplate.cs
1 using System;
2 using System.ComponentModel;
3 using System.Collections.Generic;
4
5 namespace Tizen.NUI.Binding
6 {
7     [EditorBrowsable(EditorBrowsableState.Never)]
8     public class DataTemplate : ElementTemplate
9     {
10         /// <summary>
11         /// Base constructor.
12         /// </summary>
13         [EditorBrowsable(EditorBrowsableState.Never)]
14         public DataTemplate()
15         {
16         }
17
18         /// <summary>
19         /// Base constructor with specific Type.
20         /// </summary>
21         /// <param name="Type">The Type of content.</param>
22         [EditorBrowsable(EditorBrowsableState.Never)]
23         public DataTemplate(Type type) : base(type)
24         {
25         }
26
27         /// <summary>
28         /// Base constructor with loadTemplate function.
29         /// </summary>
30         /// <param name="loadTemplate">The function of loading templated object.</param>
31         [EditorBrowsable(EditorBrowsableState.Never)]
32         public DataTemplate(Func<object> loadTemplate) : base(loadTemplate)
33         {
34         }
35
36         [EditorBrowsable(EditorBrowsableState.Never)]
37         public IDictionary<BindableProperty, BindingBase> Bindings { get; } = new Dictionary<BindableProperty, BindingBase>();
38
39         [EditorBrowsable(EditorBrowsableState.Never)]
40         public IDictionary<BindableProperty, object> Values { get; } = new Dictionary<BindableProperty, object>();
41
42         [EditorBrowsable(EditorBrowsableState.Never)]
43         public void SetBinding(BindableProperty property, BindingBase binding)
44         {
45             if (property == null)
46                 throw new ArgumentNullException(nameof(property));
47             if (binding == null)
48                 throw new ArgumentNullException(nameof(binding));
49
50             Values.Remove(property);
51             Bindings[property] = binding;
52         }
53
54         [EditorBrowsable(EditorBrowsableState.Never)]
55         public void SetValue(BindableProperty property, object value)
56         {
57             if (property == null)
58                 throw new ArgumentNullException(nameof(property));
59
60             Bindings.Remove(property);
61             Values[property] = value;
62         }
63
64         internal override void SetupContent(object item)
65         {
66             ApplyBindings(item);
67             ApplyValues(item);
68         }
69
70         void ApplyBindings(object item)
71         {
72             if (Bindings == null)
73                 return;
74
75             var bindable = item as BindableObject;
76             if (bindable == null)
77                 return;
78
79             foreach (KeyValuePair<BindableProperty, BindingBase> kvp in Bindings)
80             {
81                 if (Values.ContainsKey(kvp.Key))
82                     throw new InvalidOperationException("Binding and Value found for " + kvp.Key.PropertyName);
83
84                 bindable.SetBinding(kvp.Key, kvp.Value.Clone());
85             }
86         }
87
88         void ApplyValues(object item)
89         {
90             if (Values == null)
91                 return;
92
93             var bindable = item as BindableObject;
94             if (bindable == null)
95                 return;
96             foreach (KeyValuePair<BindableProperty, object> kvp in Values)
97                 bindable.SetValue(kvp.Key, kvp.Value);
98         }
99     }
100 }