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
24 [EditorBrowsable(EditorBrowsableState.Never)]
25 public class DataTemplate : ElementTemplate
30 [EditorBrowsable(EditorBrowsableState.Never)]
36 /// Base constructor with specific Type.
38 /// <param name="type">The Type of content.</param>
39 [EditorBrowsable(EditorBrowsableState.Never)]
40 public DataTemplate(Type type) : base(type)
45 /// Base constructor with loadTemplate function.
47 /// <param name="loadTemplate">The function of loading templated object.</param>
48 [EditorBrowsable(EditorBrowsableState.Never)]
49 public DataTemplate(Func<object> loadTemplate) : base(loadTemplate)
53 [EditorBrowsable(EditorBrowsableState.Never)]
54 public IDictionary<BindableProperty, BindingBase> Bindings { get; } = new Dictionary<BindableProperty, BindingBase>();
56 [EditorBrowsable(EditorBrowsableState.Never)]
57 public IDictionary<BindableProperty, object> Values { get; } = new Dictionary<BindableProperty, object>();
59 [EditorBrowsable(EditorBrowsableState.Never)]
60 public void SetBinding(BindableProperty property, BindingBase binding)
63 throw new ArgumentNullException(nameof(property));
65 throw new ArgumentNullException(nameof(binding));
67 Values.Remove(property);
68 Bindings[property] = binding;
71 [EditorBrowsable(EditorBrowsableState.Never)]
72 public void SetValue(BindableProperty property, object value)
75 throw new ArgumentNullException(nameof(property));
77 Bindings.Remove(property);
78 Values[property] = value;
81 internal override void SetupContent(object item)
87 void ApplyBindings(object item)
92 var bindable = item as BindableObject;
96 foreach (KeyValuePair<BindableProperty, BindingBase> kvp in Bindings)
98 if (Values.ContainsKey(kvp.Key))
99 throw new InvalidOperationException("Binding and Value found for " + kvp.Key.PropertyName);
101 bindable.SetBinding(kvp.Key, kvp.Value.Clone());
105 void ApplyValues(object item)
110 var bindable = item as BindableObject;
111 if (bindable == null)
113 foreach (KeyValuePair<BindableProperty, object> kvp in Values)
114 bindable.SetValue(kvp.Key, kvp.Value);