/// <summary> /// Adds the given <paramref name="dataBinding"/> to the internal index for /// the given data binding target <paramref name="obj"/> /// with the given bound property <paramref name="property"/>. /// </summary> /// <param name="obj">Data binding target</param> /// <param name="property">Data binding property</param> /// <param name="dataBinding">Data binding to index</param> public void AddToIndex <TValue>(IDataBindingSupport obj, IBindableProperty <TValue> property, DataBinding dataBinding) { Tuple <IDataBindingSupport, IBindableProperty> key = Tuple.Create(obj, (IBindableProperty)property); lock (_globalDataBindingCache) { _globalDataBindingCache[key] = dataBinding; } }
/// <summary> /// Returns the data binding for the given data binding target <paramref name="obj"/> for the given /// bound property <paramref name="property"/>. Returns NULL if there is no data binding. /// </summary> /// <param name="obj">Data binding target</param> /// <param name="property">Data binding property</param> /// <returns>Data binding or NULL</returns> public DataBinding GetDataBinding <TValue>(IDataBindingSupport obj, IBindableProperty <TValue> property) { Tuple <IDataBindingSupport, IBindableProperty> key = Tuple.Create(obj, (IBindableProperty)property); lock (_globalDataBindingCache) { _globalDataBindingCache.TryGetValue(key, out DataBinding dataBinding); return(dataBinding); } }
/// <summary> /// Returns the current value of the given <paramref name="property"/> /// of the given data binding target <paramref name="obj"/>. /// </summary> /// <param name="obj">Data binding target</param> /// <param name="property">Data binding property</param> /// <returns>Current property value</returns> public TValue GetValue <TValue>(IDataBindingSupport obj, IBindableProperty <TValue> property) { Tuple <IDataBindingSupport, IBindableProperty> key = Tuple.Create(obj, (IBindableProperty)property); object result; lock (_globalValueCache) { _globalValueCache.TryGetValue(key, out result); } return((TValue)result); }
/// <summary> /// Applies the default value of the given <paramref name="property"/> to the given binding target <paramref name="target"/>. /// </summary> /// <param name="target">Data binding target</param> /// <param name="property">Data binding property</param> private void ApplyDefaultPropertyValue <TValue>(IDataBindingSupport target, IBindableProperty <TValue> property) { try { if (target == null) { throw new ArgumentNullException(nameof(target)); } if (property == null) { throw new ArgumentNullException(nameof(property)); } ApplyValueChange(target, property.PropertyInfo, property.DefaultValue); } catch (Exception ex) { _log.Error("Error on applying default property value to the binding target.", ex); } }
/// <summary> /// Sets the given <paramref name="value"/> as current property value of the bound <paramref name="property"/> for /// the given binding target <paramref name="obj"/>. Returns TRUE if the (new) given value is not equal to the current one. /// Only in this case the new value is set. /// </summary> /// <param name="obj">Data binding target</param> /// <param name="property">Data binding property</param> /// <param name="value">New property value</param> /// <returns>TRUE if the new value has been set</returns> public bool SetValue <TValue>(IDataBindingSupport obj, IBindableProperty <TValue> property, TValue value) { Tuple <IDataBindingSupport, IBindableProperty> key = Tuple.Create(obj, (IBindableProperty)property); lock (_globalValueCache) { // Only update real new values _globalValueCache.TryGetValue(key, out object currentValue); if (Equals(currentValue, value)) { return(false); } // Update the new value _globalValueCache[key] = value; return(true); } }
/// <summary> /// Applies the given <paramref name="dataBinding"/> to the given <paramref name="uiElement"/> for the /// given <paramref name="property"/>. /// </summary> /// <param name="uiElement">UI Element that is used as target of the binding</param> /// <param name="property">Property that is being bound</param> /// <param name="dataBinding">Data binding</param> /// <exception cref="ArgumentNullException">If any parameter is NULL</exception> public static void Apply <TValue>(IDataBindingSupport uiElement, IBindableProperty <TValue> property, DataBinding dataBinding) { if (uiElement == null) { throw new ArgumentNullException(nameof(uiElement)); } if (property == null) { throw new ArgumentNullException(nameof(property)); } if (dataBinding == null) { throw new ArgumentNullException(nameof(dataBinding)); } dataBinding.Connect(uiElement, property); GlobalDataBindingIndex.Instance.AddToIndex(uiElement, property, dataBinding); }
/// <summary> /// Connects this instance to the given data binding <paramref name="target"/> for the <paramref name="property"/>. /// </summary> /// <param name="target">Data binding target to connect</param> /// <param name="property">Target property to bind</param> public void Connect <TValue>(IDataBindingSupport target, IBindableProperty <TValue> property) { Target = target ?? throw new ArgumentNullException(nameof(target)); TargetPropertyInfo = (property ?? throw new ArgumentNullException(nameof(property))).PropertyInfo; // Apply default value to the target ApplyDefaultPropertyValue(target, property); // Selected binding mode EDataBindingMode bindingMode = BindingMode; if (bindingMode == EDataBindingMode.Default) { bindingMode = (property.BindsTwoWayByDefault ? EDataBindingMode.TwoWay : EDataBindingMode.OneWay); } // Set property ShallUpdateSource = (bindingMode == EDataBindingMode.OneWayToSource || bindingMode == EDataBindingMode.TwoWay); // OneWayToSource if (bindingMode == EDataBindingMode.OneWayToSource) { // Update source immediately ApplyValueToBindingSource(Source, SourcePropertyInfo, Target, property.PropertyInfo); return; } // Update immediately the target ApplyValueToBindingTarget(target, property.PropertyInfo, Source, SourcePropertyInfo); // In this case we're finished here if (bindingMode == EDataBindingMode.OneTime) { return; } // OneWay and/or TwoWay // Add update listener to the source (if supported) if (Source is INotifyPropertyChanged propertyChangedDispatcher) { propertyChangedDispatcher.PropertyChanged += OnSourcePropertyChanged; } }
/// <summary> /// Removes all data bindings for the given data binding /// target <paramref name="obj"/> from the index. /// </summary> /// <param name="obj"></param> public void RemoveFromIndex(IDataBindingSupport obj) { // Remove cached values lock (_globalValueCache) { Tuple <IDataBindingSupport, IBindableProperty>[] keysToRemove = _globalValueCache.Keys.Where(k => Equals(obj, k.Item1)).ToArray(); for (short i = -1; ++i != keysToRemove.Length;) { Tuple <IDataBindingSupport, IBindableProperty> key = keysToRemove[i]; _globalValueCache.Remove(key); } } lock (_globalDataBindingCache) { Tuple <IDataBindingSupport, IBindableProperty>[] keysToRemove = _globalDataBindingCache.Keys.Where(k => Equals(obj, k.Item1)).ToArray(); for (short i = -1; ++i != keysToRemove.Length;) { Tuple <IDataBindingSupport, IBindableProperty> key = keysToRemove[i]; DataBinding dataBinding = _globalDataBindingCache[key]; dataBinding.Dispose(); _globalDataBindingCache.Remove(key); } } }