54664da18f6ecd45bf9518d8654e1e274b5e5213
[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     /// <summary>
25     /// A template for multiple bindings, commonly used by RecylerView and CollectionView.
26     /// </summary>
27     /// <since_tizen> 9 </since_tizen>
28     public class DataTemplate : ElementTemplate
29     {
30         /// <summary>
31         /// Base constructor.
32         /// </summary>
33         /// <since_tizen> 9 </since_tizen>
34         public DataTemplate()
35         {
36         }
37
38         /// <summary>
39         /// Base constructor with specific Type.
40         /// </summary>
41         /// <param name="type">The Type of content.</param>
42         /// <since_tizen> 9 </since_tizen>
43         public DataTemplate(Type type) : base(type)
44         {
45         }
46
47         /// <summary>
48         /// Base constructor with loadTemplate function.
49         /// </summary>
50         /// <param name="loadTemplate">The function of loading templated object.</param>
51          /// <since_tizen> 9 </since_tizen>
52         public DataTemplate(Func<object> loadTemplate) : base(loadTemplate)
53         {
54         }
55
56         /// <summary>
57         /// Gets a dictionary of bindings, indexed by the bound properties.
58         /// </summary>
59         /// <since_tizen> 9 </since_tizen>
60         public IDictionary<BindableProperty, BindingBase> Bindings { get; } = new Dictionary<BindableProperty, BindingBase>();
61
62
63         /// <summary>
64         /// Returns a dictionary of property values for this DataTemplate, indexed by property.
65         /// </summary>
66         /// <since_tizen> 9 </since_tizen>
67         public IDictionary<BindableProperty, object> Values { get; } = new Dictionary<BindableProperty, object>();
68
69
70         /// <summary>
71         /// Sets the binding for property.
72         /// </summary>
73         /// <param name="property">The property to which to bind.</param>
74         /// <param name="binding">The binding to use.</param>
75         /// <since_tizen> 9 </since_tizen>
76         public void SetBinding(BindableProperty property, BindingBase binding)
77         {
78             if (property == null)
79                 throw new ArgumentNullException(nameof(property));
80             if (binding == null)
81                 throw new ArgumentNullException(nameof(binding));
82
83             Values.Remove(property);
84             Bindings[property] = binding;
85         }
86
87         /// <summary>
88         /// Sets the value of property.
89         /// </summary>
90         /// <param name="property">The property to set.</param>
91         /// <param name="value">The new value.</param>
92         /// <since_tizen> 9 </since_tizen>
93         [EditorBrowsable(EditorBrowsableState.Never)]
94         public void SetValue(BindableProperty property, object value)
95         {
96             if (property == null)
97                 throw new ArgumentNullException(nameof(property));
98
99             Bindings.Remove(property);
100             Values[property] = value;
101         }
102
103         internal override void SetupContent(object item)
104         {
105             ApplyBindings(item);
106             ApplyValues(item);
107         }
108
109         void ApplyBindings(object item)
110         {
111             if (Bindings == null)
112                 return;
113
114             var bindable = item as BindableObject;
115             if (bindable == null)
116                 return;
117
118             foreach (KeyValuePair<BindableProperty, BindingBase> kvp in Bindings)
119             {
120                 if (Values.ContainsKey(kvp.Key))
121                     throw new InvalidOperationException("Binding and Value found for " + kvp.Key.PropertyName);
122
123                 bindable.SetBinding(kvp.Key, kvp.Value.Clone());
124             }
125         }
126
127         void ApplyValues(object item)
128         {
129             if (Values == null)
130                 return;
131
132             var bindable = item as BindableObject;
133             if (bindable == null)
134                 return;
135             foreach (KeyValuePair<BindableProperty, object> kvp in Values)
136                 bindable.SetValue(kvp.Key, kvp.Value);
137         }
138     }
139 }