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.Collections.Generic;
20 using System.ComponentModel;
21 using System.Globalization;
23 using System.Linq.Expressions;
24 using System.Reflection;
26 using System.Diagnostics.CodeAnalysis;
28 namespace Tizen.NUI.Binding
30 /// This will be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API.
31 [SuppressMessage("Microsoft.Design", "CA1724: Type names should not match namespaces")]
32 [EditorBrowsable(EditorBrowsableState.Never)]
33 public sealed class Binding : BindingBase
35 internal const string SelfPath = ".";
36 IValueConverter converter;
37 object converterParameter;
39 BindingExpression expression;
42 string updateSourceEventName;
44 /// This will be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API.
45 [EditorBrowsable(EditorBrowsableState.Never)]
50 /// This will be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API.
51 [EditorBrowsable(EditorBrowsableState.Never)]
52 public Binding(string path, BindingMode mode = BindingMode.Default, IValueConverter converter = null, object converterParameter = null, string stringFormat = null, object source = null)
55 throw new ArgumentNullException(nameof(path));
56 if (string.IsNullOrWhiteSpace(path))
57 throw new ArgumentException("path can not be an empty string", nameof(path));
60 Converter = converter;
61 ConverterParameter = converterParameter;
63 StringFormat = stringFormat;
67 /// This will be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API.
68 [EditorBrowsable(EditorBrowsableState.Never)]
69 public IValueConverter Converter
71 get { return converter; }
80 /// This will be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API.
81 [EditorBrowsable(EditorBrowsableState.Never)]
82 public object ConverterParameter
84 get { return converterParameter; }
89 converterParameter = value;
93 /// This will be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API.
94 [EditorBrowsable(EditorBrowsableState.Never)]
103 expression = new BindingExpression(this, !string.IsNullOrWhiteSpace(value) ? value : SelfPath);
107 /// This will be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API.
108 [EditorBrowsable(EditorBrowsableState.Never)]
111 get { return source; }
117 if (source is BindableObject bindableObject)
119 bindableObject.IsBound = true;
124 /// This will be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API.
125 [EditorBrowsable(EditorBrowsableState.Never)]
126 public string UpdateSourceEventName
128 get { return updateSourceEventName; }
132 updateSourceEventName = value;
136 internal override void Apply(bool fromTarget)
138 base.Apply(fromTarget);
140 if (expression == null)
141 expression = new BindingExpression(this, SelfPath);
143 expression.Apply(fromTarget);
146 internal override void Apply(object newContext, BindableObject bindObj, BindableProperty targetProperty, bool fromBindingContextChanged = false)
149 var isApplied = IsApplied;
151 base.Apply(src ?? newContext, bindObj, targetProperty, fromBindingContextChanged: fromBindingContextChanged);
153 if (src != null && isApplied && fromBindingContextChanged)
156 object bindingContext = src ?? Context ?? newContext;
157 if (expression == null && bindingContext != null)
158 expression = new BindingExpression(this, SelfPath);
160 expression?.Apply(bindingContext, bindObj, targetProperty);
163 internal override BindingBase Clone()
165 return new Binding(Path, Mode) { Converter = Converter, ConverterParameter = ConverterParameter, StringFormat = StringFormat, Source = Source, UpdateSourceEventName = UpdateSourceEventName };
168 internal override object GetSourceValue(object value, Type targetPropertyType)
170 if (Converter != null)
171 value = Converter.Convert(value, targetPropertyType, ConverterParameter, CultureInfo.CurrentUICulture);
173 return base.GetSourceValue(value, targetPropertyType);
176 internal override object GetTargetValue(object value, Type sourcePropertyType)
178 if (Converter != null)
179 value = Converter.ConvertBack(value, sourcePropertyType, ConverterParameter, CultureInfo.CurrentUICulture);
181 return base.GetTargetValue(value, sourcePropertyType);
184 internal override void Unapply(bool fromBindingContextChanged = false)
186 if (Source != null && fromBindingContextChanged && IsApplied)
189 base.Unapply(fromBindingContextChanged: fromBindingContextChanged);
191 if (expression != null)
192 expression.Unapply();