Пример #1
0
        /// <summary>
        /// Creates a property meta data descriptor for the public property with the given <paramref name="propertyRef"/> of the associated
        /// <typeparamref name="TControl"/> type.
        /// </summary>
        /// <typeparam name="TValue">Type of property value</typeparam>
        /// <param name="propertyRef">Reference of the property</param>
        /// <param name="defaultValue">Default property value</param>
        /// <param name="bindsTwoWayByDefault">If TRUE the property supports a two-way-binding per default</param>
        /// <param name="defaultSourceUpdateTrigger">Sets the default source update trigger</param>
        /// <returns>Created property meta data descriptor</returns>
        protected static IBindableProperty <TValue> CreatePropertyInfo <TValue>(Expression <Func <TControl, TValue> > propertyRef, TValue defaultValue, bool bindsTwoWayByDefault = false,
                                                                                EDataBindingSourceUpdateTrigger defaultSourceUpdateTrigger = null)
        {
            string propertyName = propertyRef.GetPropertyName();

            return(CreatePropertyInfo(propertyName, defaultValue, bindsTwoWayByDefault, defaultSourceUpdateTrigger));
        }
Пример #2
0
        /// <summary>
        /// Sets the current model value for the given data binding target <paramref name="obj"/> and the given <paramref name="property"/>
        /// to the given <paramref name="value"/>. Additionally an <paramref name="updateReason"/> has to be given!
        /// </summary>
        /// <typeparam name="TValue">Type of the model value</typeparam>
        /// <param name="obj">Data binding target </param>
        /// <param name="property">Data binding property</param>
        /// <param name="value">New value to set</param>
        /// <param name="updateReason">Update reason - must not be NULL</param>
        /// <exception cref="ArgumentNullException">If <paramref name="updateReason"/> is NULL</exception>
        public static void SetModelValue <TValue>(TControl obj, IBindableProperty <TValue> property, TValue value, // Called from control
                                                  EBindingSourceUpdateReason updateReason)
        {
            if (updateReason == null)
            {
                throw new ArgumentNullException("updateReason");
            }

            // First, update the value
            if (!GlobalDataBindingIndex.Instance.SetValue(obj, property, value))
            {
                return;
            }

            // If there is no data binding there is no model to update
            DataBinding dataBinding = GlobalDataBindingIndex.Instance.GetDataBinding(obj, property);

            if (dataBinding == null)
            {
                return;                      // TODO Check if correct
            }
            // If set to default the default setting of the property is used
            EDataBindingSourceUpdateTrigger updateSourceTrigger = dataBinding.BindingSourceUpdateTrigger;

            if (updateSourceTrigger == EDataBindingSourceUpdateTrigger.Default)
            {
                updateSourceTrigger = property.DefaultUpdateSourceUpdateTrigger;
            }

            // Check if reason fits to set trigger
            if (updateReason == EBindingSourceUpdateReason.PropertyChanged && updateSourceTrigger != EDataBindingSourceUpdateTrigger.PropertyChanged)
            {
                return;
            }
            if (updateReason == EBindingSourceUpdateReason.LostFocus && updateSourceTrigger != EDataBindingSourceUpdateTrigger.LostFocus)
            {
                return;
            }

            // Update the source
            dataBinding.UpdateSource(obj, property, value);
        }
Пример #3
0
 /// <summary>
 /// Creates a property meta data descriptor for the public property with the given <paramref name="propertyName"/> of the associated
 /// <typeparamref name="TControl"/> type.
 /// </summary>
 /// <typeparam name="TValue">Type of property value</typeparam>
 /// <param name="propertyName">Name of the property</param>
 /// <param name="defaultValue">Default property value</param>
 /// <param name="bindsTwoWayByDefault">If TRUE the property supports a two-way-binding per default</param>
 /// <param name="defaultSourceUpdateTrigger">Sets the default source update trigger</param>
 /// <returns>Created property meta data descriptor</returns>
 protected static IBindableProperty <TValue> CreatePropertyInfo <TValue>(string propertyName, TValue defaultValue, bool bindsTwoWayByDefault = false,
                                                                         EDataBindingSourceUpdateTrigger defaultSourceUpdateTrigger          = null)
 {
     return(PropertyDescriptor.CreateInfo(typeof(TControl), propertyName, defaultValue, bindsTwoWayByDefault, defaultSourceUpdateTrigger));
 }
Пример #4
0
        /// <summary> Creates an instance of <see cref="IBindableProperty"/> based of the given parameters. </summary>
        /// <param name="controlType">Type of the control that provides the property</param>
        /// <param name="propertyName">Name of the property</param>
        /// <param name="defaultValue">Advised the default (initial) value</param>
        /// <param name="bindsTwoByDefault">If TRUE the property advised to support two-way-binding at default</param>
        /// <param name="defaultSourceUpdateTrigger">Advised source update trigger for the property. Must not be <see cref="EDataBindingSourceUpdateTrigger.Default"/>.</param>
        /// <returns>Newly created instance of <see cref="IBindableProperty"/></returns>
        /// <exception cref="ArgumentNullException">If any parameter is NULL</exception>
        /// <exception cref="ArgumentException">If <paramref name="defaultSourceUpdateTrigger"/> is <see cref="EDataBindingSourceUpdateTrigger.Default"/></exception>
        public static IBindableProperty <TValue> CreateInfo <TValue>(Type controlType, string propertyName, TValue defaultValue, bool bindsTwoByDefault = false, EDataBindingSourceUpdateTrigger defaultSourceUpdateTrigger = null)
        {
            if (controlType == null)
            {
                throw new ArgumentNullException(nameof(controlType));
            }
            if (string.IsNullOrEmpty(propertyName))
            {
                throw new ArgumentNullException(nameof(propertyName));
            }

            if (defaultSourceUpdateTrigger == EDataBindingSourceUpdateTrigger.Default)
            {
                throw new ArgumentException($"Default source update trigger cannot be '{EDataBindingSourceUpdateTrigger.Default}'!");
            }

            PropertyInfo[] controlTypeProperties = controlType.GetPublicProperties();
            PropertyInfo   propertyInfo          = controlTypeProperties.First(property => property.Name == propertyName);

            return(new BindablePropertyImpl <TValue>(controlType, propertyName, propertyInfo)
            {
                BindsTwoWayByDefault = bindsTwoByDefault,
                DefaultUpdateSourceUpdateTrigger = defaultSourceUpdateTrigger ?? EDataBindingSourceUpdateTrigger.PropertyChanged,
                DefaultValue = defaultValue
            });
        }