2 * Copyright(c) 2021 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 using System.ComponentModel;
20 using System.Collections.Generic;
22 namespace Tizen.NUI.Binding
25 /// A template for multiple bindings, commonly used by RecylerView and CollectionView.
27 /// <since_tizen> 9 </since_tizen>
28 public class DataTemplate : ElementTemplate
33 /// <since_tizen> 9 </since_tizen>
39 /// Base constructor with specific Type.
41 /// <param name="type">The Type of content.</param>
42 /// <since_tizen> 9 </since_tizen>
43 public DataTemplate(Type type) : base(type)
48 /// Base constructor with loadTemplate function.
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)
57 /// Gets a dictionary of bindings, indexed by the bound properties.
59 /// <since_tizen> 9 </since_tizen>
60 public IDictionary<BindableProperty, BindingBase> Bindings { get; } = new Dictionary<BindableProperty, BindingBase>();
64 /// Returns a dictionary of property values for this DataTemplate, indexed by property.
66 /// <since_tizen> 9 </since_tizen>
67 public IDictionary<BindableProperty, object> Values { get; } = new Dictionary<BindableProperty, object>();
71 /// Sets the binding for property.
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)
79 throw new ArgumentNullException(nameof(property));
81 throw new ArgumentNullException(nameof(binding));
83 Values.Remove(property);
84 Bindings[property] = binding;
88 /// Sets the value of property.
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)
97 throw new ArgumentNullException(nameof(property));
99 Bindings.Remove(property);
100 Values[property] = value;
103 internal override void SetupContent(object item)
109 void ApplyBindings(object item)
111 if (Bindings == null)
114 var bindable = item as BindableObject;
115 if (bindable == null)
118 foreach (KeyValuePair<BindableProperty, BindingBase> kvp in Bindings)
120 if (Values.ContainsKey(kvp.Key))
121 throw new InvalidOperationException("Binding and Value found for " + kvp.Key.PropertyName);
123 bindable.SetBinding(kvp.Key, kvp.Value.Clone());
127 void ApplyValues(object item)
132 var bindable = item as BindableObject;
133 if (bindable == null)
135 foreach (KeyValuePair<BindableProperty, object> kvp in Values)
136 bindable.SetValue(kvp.Key, kvp.Value);