Ejemplo n.º 1
0
        /// <summary>
        /// Shows the specified o.
        /// </summary>
        /// <param name="o">The o.</param>
        public void Show(Object o)
        {
            if (o == null)
            {
                throw new ArgumentNullException("o");
            }

            var controls = new List <IBindable>();

            ControlHelper.FindControlsRecursive <IBindable>(Controls, controls);

            var type = o.GetType();

            foreach (var control in controls)
            {
                var propertyName = control.PropertyName;
                if (string.IsNullOrEmpty(propertyName))
                {
                    continue;
                }

                var property = type.GetProperty(propertyName);
                if (property == null)
                {
                    throw new InvalidOperationException("Error databinding. Property not exists " + propertyName);
                }
                control.SetValue(property.GetValue(o, null));
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Fills the specified o.
        /// </summary>
        /// <param name="o">The o.</param>
        public void Fill(Object o)
        {
            if (o == null)
            {
                throw new ArgumentNullException("o");
            }

            var controls = new List <IBindable>();

            ControlHelper.FindControlsRecursive <IBindable>(Controls, controls);

            var type = o.GetType();

            foreach (IBindable control in controls)
            {
                var propertyName = control.PropertyName;
                if (!control.CanGetValue || string.IsNullOrEmpty(propertyName))
                {
                    continue;
                }


                var property = type.GetProperty(propertyName);
                if (property == null)
                {
                    throw new InvalidOperationException(string.Format("Error bind property {0}. Property not found.",
                                                                      propertyName));
                }
                if (!property.CanRead)
                {
                    throw new InvalidOperationException(string.Format("Error bind property {0}. Property write only.",
                                                                      propertyName));
                }
                var value = control.GetValue();
                try
                {
                    property.SetValue(o, Convert.ChangeType(value, property.PropertyType), null);
                }
                catch (Exception ex)
                {
                    throw new InvalidOperationException(string.Format("Error bind property '{0}'. Can't set value '{1}'.",
                                                                      propertyName, value), ex);
                }
            }
        }