2 using System.Globalization;
4 namespace Tizen.NUI.XamlBinding
6 internal class TemplateBinding : BindingBase
8 internal const string SelfPath = ".";
9 IValueConverter _converter;
10 object _converterParameter;
12 BindingExpression _expression;
15 public TemplateBinding()
19 public TemplateBinding(string path, BindingMode mode = BindingMode.Default, IValueConverter converter = null, object converterParameter = null, string stringFormat = null)
22 throw new ArgumentNullException("path");
23 if (string.IsNullOrWhiteSpace(path))
24 throw new ArgumentException("path can not be an empty string", "path");
28 Converter = converter;
29 ConverterParameter = converterParameter;
31 StringFormat = stringFormat;
34 public IValueConverter Converter
36 get { return _converter; }
45 public object ConverterParameter
47 get { return _converterParameter; }
52 _converterParameter = value;
64 _expression = GetBindingExpression(value);
68 internal override void Apply(bool fromTarget)
70 base.Apply(fromTarget);
72 if (_expression == null)
73 _expression = new BindingExpression(this, SelfPath);
75 _expression.Apply(fromTarget);
78 internal override async void Apply(object newContext, BindableObject bindObj, BindableProperty targetProperty, bool fromBindingContextChanged = false)
80 var view = bindObj as Element;
82 throw new InvalidOperationException();
84 base.Apply(newContext, bindObj, targetProperty, fromBindingContextChanged);
86 Element templatedParent = await TemplateUtilities.FindTemplatedParentAsync(view);
87 ApplyInner(templatedParent, bindObj, targetProperty);
90 internal override BindingBase Clone()
92 return new TemplateBinding(Path, Mode) { Converter = Converter, ConverterParameter = ConverterParameter, StringFormat = StringFormat };
95 internal override object GetSourceValue(object value, Type targetPropertyType)
97 if (Converter != null)
98 value = Converter.Convert(value, targetPropertyType, ConverterParameter, CultureInfo.CurrentUICulture);
100 return base.GetSourceValue(value, targetPropertyType);
103 internal override object GetTargetValue(object value, Type sourcePropertyType)
105 if (Converter != null)
106 value = Converter.ConvertBack(value, sourcePropertyType, ConverterParameter, CultureInfo.CurrentUICulture);
108 return base.GetTargetValue(value, sourcePropertyType);
111 internal override void Unapply(bool fromBindingContextChanged = false)
113 base.Unapply(fromBindingContextChanged: fromBindingContextChanged);
115 if (_expression != null)
116 _expression.Unapply();
119 void ApplyInner(Element templatedParent, BindableObject bindableObject, BindableProperty targetProperty)
121 if (_expression == null && templatedParent != null)
122 _expression = new BindingExpression(this, SelfPath);
124 _expression?.Apply(templatedParent, bindableObject, targetProperty);
127 BindingExpression GetBindingExpression(string path)
129 return new BindingExpression(this, !string.IsNullOrWhiteSpace(path) ? path : SelfPath);