/* * Copyright(c) 2021 Samsung Electronics Co., Ltd. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * */ using System; using System.ComponentModel; using System.Collections.Generic; namespace Tizen.NUI.Binding { /// /// A template for multiple bindings, commonly used by RecylerView and CollectionView. /// /// 9 public class DataTemplate : ElementTemplate { /// /// Base constructor. /// /// 9 public DataTemplate() { } /// /// Base constructor with specific Type. /// /// The Type of content. /// 9 public DataTemplate(Type type) : base(type) { } /// /// Base constructor with loadTemplate function. /// /// The function of loading templated object. /// 9 public DataTemplate(Func loadTemplate) : base(loadTemplate) { } /// /// Gets a dictionary of bindings, indexed by the bound properties. /// /// 9 public IDictionary Bindings { get; } = new Dictionary(); /// /// Returns a dictionary of property values for this DataTemplate, indexed by property. /// /// 9 public IDictionary Values { get; } = new Dictionary(); /// /// Sets the binding for property. /// /// The property to which to bind. /// The binding to use. /// 9 public void SetBinding(BindableProperty property, BindingBase binding) { if (property == null) throw new ArgumentNullException(nameof(property)); if (binding == null) throw new ArgumentNullException(nameof(binding)); Values.Remove(property); Bindings[property] = binding; } /// /// Sets the value of property. /// /// The property to set. /// The new value. /// 9 [EditorBrowsable(EditorBrowsableState.Never)] public void SetValue(BindableProperty property, object value) { if (property == null) throw new ArgumentNullException(nameof(property)); Bindings.Remove(property); Values[property] = value; } internal override void SetupContent(object item) { ApplyBindings(item); ApplyValues(item); } void ApplyBindings(object item) { if (Bindings == null) return; var bindable = item as BindableObject; if (bindable == null) return; foreach (KeyValuePair kvp in Bindings) { if (Values.ContainsKey(kvp.Key)) throw new InvalidOperationException("Binding and Value found for " + kvp.Key.PropertyName); bindable.SetBinding(kvp.Key, kvp.Value.Clone()); } } void ApplyValues(object item) { if (Values == null) return; var bindable = item as BindableObject; if (bindable == null) return; foreach (KeyValuePair kvp in Values) bindable.SetValue(kvp.Key, kvp.Value); } } }