Beispiel #1
0
        /// <summary>
        /// binds data from a gtk widget (e.g. entry) to a custom object property
        /// </summary>
        /// <param name='container'>
        /// a container with a "DataObjects" property that contains the target objects which receive data
        /// </param>
        /// <param name='dataholder'>
        /// a object (control) containing the source data
        /// </param>
        /// <param name='property'>
        /// propertyname wich recieves the date
        /// </param>
        public void BindData(object container, object dataholder, string property)
        {
            try
            {
                PropertyInfo pi = container.GetType().GetProperty("DataObjects");
                if (pi == null)
                {
                    throw new Exception("Container does not have a DataObjects property");
                }

                DataObjectContainer dataobjects = (pi.GetValue(container, null) as DataObjectContainer);
                if (dataobjects == null)
                {
                    throw new Exception("Could not get DataObjects Property, this must be a  Dictionary<String,object> ");
                }

                object target = null;
                String key    = getDataObjectKey(property);
                if (dataobjects.ContainsKey(key))
                {
                    target = dataobjects.Get(key);
                }

                if (target == null)
                {
                    throw new Exception("Could not find target object: " + key);
                }


                if (target is Sharpend.Databinding.BindableData)
                {
                    //check if we are binding in the other direction
                    if ((target as Sharpend.Databinding.BindableData).IsBinding)
                    {
                        return;
                    }
                }

                String       tp  = getTargetPropertyName(property);
                PropertyInfo pi2 = target.GetType().GetProperty(tp);
                if (pi2 == null)
                {
                    throw new Exception("Container does not have a DataObjects property");
                }

                object val = getValue(dataholder);
                pi2.SetValue(target, val, null);
            } catch (Exception ex)
            {
                if (OnBindException != null)
                {
                    OnBindException(ex, new EventArgs());
                }
                else
                {
                    throw;
                }
            }
        }