27ce4adbaeeed227452328fc8be19d0aa9eca630
[platform/core/csapi/tizenfx.git] / src / Tizen.NUI / src / public / Template / DataTemplate.cs
1 /*
2  * Copyright(c) 2021 Samsung Electronics Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */
17
18 using System;
19 using System.ComponentModel;
20 using System.Collections.Generic;
21
22 namespace Tizen.NUI.Binding
23 {
24     [EditorBrowsable(EditorBrowsableState.Never)]
25     public class DataTemplate : ElementTemplate
26     {
27         /// <summary>
28         /// Base constructor.
29         /// </summary>
30         [EditorBrowsable(EditorBrowsableState.Never)]
31         public DataTemplate()
32         {
33         }
34
35         /// <summary>
36         /// Base constructor with specific Type.
37         /// </summary>
38         /// <param name="type">The Type of content.</param>
39         [EditorBrowsable(EditorBrowsableState.Never)]
40         public DataTemplate(Type type) : base(type)
41         {
42         }
43
44         /// <summary>
45         /// Base constructor with loadTemplate function.
46         /// </summary>
47         /// <param name="loadTemplate">The function of loading templated object.</param>
48         [EditorBrowsable(EditorBrowsableState.Never)]
49         public DataTemplate(Func<object> loadTemplate) : base(loadTemplate)
50         {
51         }
52
53         [EditorBrowsable(EditorBrowsableState.Never)]
54         public IDictionary<BindableProperty, BindingBase> Bindings { get; } = new Dictionary<BindableProperty, BindingBase>();
55
56         [EditorBrowsable(EditorBrowsableState.Never)]
57         public IDictionary<BindableProperty, object> Values { get; } = new Dictionary<BindableProperty, object>();
58
59         [EditorBrowsable(EditorBrowsableState.Never)]
60         public void SetBinding(BindableProperty property, BindingBase binding)
61         {
62             if (property == null)
63                 throw new ArgumentNullException(nameof(property));
64             if (binding == null)
65                 throw new ArgumentNullException(nameof(binding));
66
67             Values.Remove(property);
68             Bindings[property] = binding;
69         }
70
71         [EditorBrowsable(EditorBrowsableState.Never)]
72         public void SetValue(BindableProperty property, object value)
73         {
74             if (property == null)
75                 throw new ArgumentNullException(nameof(property));
76
77             Bindings.Remove(property);
78             Values[property] = value;
79         }
80
81         internal override void SetupContent(object item)
82         {
83             ApplyBindings(item);
84             ApplyValues(item);
85         }
86
87         void ApplyBindings(object item)
88         {
89             if (Bindings == null)
90                 return;
91
92             var bindable = item as BindableObject;
93             if (bindable == null)
94                 return;
95
96             foreach (KeyValuePair<BindableProperty, BindingBase> kvp in Bindings)
97             {
98                 if (Values.ContainsKey(kvp.Key))
99                     throw new InvalidOperationException("Binding and Value found for " + kvp.Key.PropertyName);
100
101                 bindable.SetBinding(kvp.Key, kvp.Value.Clone());
102             }
103         }
104
105         void ApplyValues(object item)
106         {
107             if (Values == null)
108                 return;
109
110             var bindable = item as BindableObject;
111             if (bindable == null)
112                 return;
113             foreach (KeyValuePair<BindableProperty, object> kvp in Values)
114                 bindable.SetValue(kvp.Key, kvp.Value);
115         }
116     }
117 }