Example #1
0
            private void OnMemoChanged()
            {
                Type         type     = control.GetType();
                PropertyInfo property = type.GetProperty(PropertyName, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);

                if (property != null)
                {
                    property.SetValue(control, Memo, null);
                }
            }
Example #2
0
        public static Binding AddDataBinding <Ds, Dp>(
            IBindableComponent control,
            Ds data, Expression <Func <Ds, Dp> > dataProperty)
        {
            var    dataMemberBody = (MemberExpression)dataProperty.Body;
            var    dataMemberName = dataMemberBody.Member.Name;
            string name           = null;
            // Add TextBox.
            var textBox = control as TextBox;

            if (textBox != null)
            {
                name = nameof(textBox.Text);
            }
            // Add ComboBox.
            var comboBox = control as ComboBox;

            if (comboBox != null)
            {
                name = string.IsNullOrEmpty(comboBox.ValueMember)
                                        ? nameof(comboBox.SelectedItem)
                                        : nameof(comboBox.SelectedValue);
            }
            // Add CheckBox.
            var checkBox = control as CheckBox;

            if (checkBox != null)
            {
                name = nameof(checkBox.Checked);
            }
            // Add NumericUpDown.
            var upDown = control as NumericUpDown;

            if (upDown != null)
            {
                name = nameof(upDown.Value);
            }
            // If type is missing then throw error.
            if (string.IsNullOrEmpty(name))
            {
                throw new Exception(string.Format("Add control Type '{0}' to ControlsHelper.AddDataBinding(control, data, dataProperty) method!", control.GetType()));
            }
            // Add data binding.
            return(control.DataBindings.Add(name, data, dataMemberName,
                                            false,
                                            DataSourceUpdateMode.OnPropertyChanged,
                                            null,
                                            null,
                                            null));
        }
Example #3
0
        private void CheckBinding()
        {
            this.bindToObject.CheckBinding();

            if (control != null && propertyName.Length > 0)
            {
                control.DataBindings.CheckDuplicates(this);

                Type controlClass = control.GetType();

                // Check Properties
                string                       propertyNameIsNull = propertyName + "IsNull";
                Type                         propType           = null;
                PropertyDescriptor           tempPropInfo       = null;
                PropertyDescriptor           tempPropIsNullInfo = null;
                PropertyDescriptorCollection propInfos;

                // If the control is being inherited, then get the properties for
                // the control's type rather than for the control itself.  Getting
                // properties for the control will merge the control's properties with
                // those of its designer.  Normally we want that, but for
                // inherited controls we don't because an inherited control should
                // "act" like a runtime control.
                //
                InheritanceAttribute attr = (InheritanceAttribute)TypeDescriptor.GetAttributes(control)[typeof(InheritanceAttribute)];
                if (attr != null && attr.InheritanceLevel != InheritanceLevel.NotInherited)
                {
                    propInfos = TypeDescriptor.GetProperties(controlClass);
                }
                else
                {
                    propInfos = TypeDescriptor.GetProperties(control);
                }

                for (int i = 0; i < propInfos.Count; i++)
                {
                    if (tempPropInfo == null && string.Equals(propInfos[i].Name, propertyName, StringComparison.OrdinalIgnoreCase))
                    {
                        tempPropInfo = propInfos[i];
                        if (tempPropIsNullInfo != null)
                        {
                            break;
                        }
                    }
                    if (tempPropIsNullInfo == null && string.Equals(propInfos[i].Name, propertyNameIsNull, StringComparison.OrdinalIgnoreCase))
                    {
                        tempPropIsNullInfo = propInfos[i];
                        if (tempPropInfo != null)
                        {
                            break;
                        }
                    }
                }

                if (tempPropInfo == null)
                {
                    throw new ArgumentException(string.Format(SR.ListBindingBindProperty, propertyName), "PropertyName");
                }
                if (tempPropInfo.IsReadOnly && this.controlUpdateMode != ControlUpdateMode.Never)
                {
                    throw new ArgumentException(string.Format(SR.ListBindingBindPropertyReadOnly, propertyName), "PropertyName");
                }

                propInfo          = tempPropInfo;
                propType          = propInfo.PropertyType;
                propInfoConverter = propInfo.Converter;

                if (tempPropIsNullInfo != null && tempPropIsNullInfo.PropertyType == typeof(bool) && !tempPropIsNullInfo.IsReadOnly)
                {
                    propIsNullInfo = tempPropIsNullInfo;
                }

                // Check events
                EventDescriptor           tempValidateInfo = null;
                string                    validateName     = "Validating";
                EventDescriptorCollection eventInfos       = TypeDescriptor.GetEvents(control);
                for (int i = 0; i < eventInfos.Count; i++)
                {
                    if (tempValidateInfo == null && string.Equals(eventInfos[i].Name, validateName, StringComparison.OrdinalIgnoreCase))
                    {
                        tempValidateInfo = eventInfos[i];
                        break;
                    }
                }
                validateInfo = tempValidateInfo;
            }
            else
            {
                propInfo     = null;
                validateInfo = null;
            }

            // go see if we become bound now.
            UpdateIsBinding();
        }