public void Add_NullDataBinding_ThrowsArgumentNullException()
        {
            var control    = new Control();
            var collection = new ControlBindingsCollection(control);

            Assert.Throws <ArgumentNullException>("dataBinding", () => collection.Add(null));
        }
Esempio n. 2
0
        public void PropertyManager_EndCurrentEdit_IEditableObjectCurrentNotSuccess_DoesNotCallEndEdit(bool cancel, int expectedCallCount)
        {
            int callCount  = 0;
            var dataSource = new EditableDataSource
            {
                EndEditHandler = () =>
                {
                    callCount++;
                }
            };

            var manager = new PropertyManager(dataSource);
            var control = new SubControl {
                Visible = true
            };

            control.CreateControl();
            var controlBindings = new ControlBindingsCollection(control);
            var cancelBinding   = new Binding("Value", dataSource, "Property", true);
            BindingCompleteEventHandler bindingCompleteHandler = (sender, e) =>
            {
                e.Cancel = cancel;
            };

            cancelBinding.BindingComplete += bindingCompleteHandler;
            controlBindings.Add(cancelBinding);
            manager.Bindings.Add(cancelBinding);
            manager.EndCurrentEdit();
            Assert.Equal(expectedCallCount, callCount);

            manager.EndCurrentEdit();
            Assert.Equal(expectedCallCount * 2, callCount);
        }
 public static void AddIfValid(this ControlBindingsCollection dataSource, string controlProperty, BindingSource binding, string bizoProperty)
 {
     if (!string.IsNullOrEmpty(bizoProperty))
     {
         dataSource.Add(controlProperty, binding, bizoProperty);
     }
 }
        public static Binding Add <T>(
            [NotNull][ItemCanBeNull] this ControlBindingsCollection bindingCollection,
            [NotNull] string property,
            [NotNull] object datasource,
            [NotNull] Expression <Func <T, object> > expression)
        {
            Contract.Requires(bindingCollection != null);
            Contract.Requires(property != null);
            Contract.Requires(datasource != null);
            Contract.Requires(expression != null);
            Contract.Ensures(Contract.Result <Binding>() != null);

            string relatedNameChain;

            switch (expression.Body)
            {
            case UnaryExpression body:
                relatedNameChain = (body.Operand as MemberExpression)?.ToString() ?? string.Empty;
                break;

            case MemberExpression body:
                relatedNameChain = body.ToString();
                break;

            default:
                relatedNameChain = string.Empty;
                break;
            }

            var skippedName = string.Join(".", relatedNameChain.Split('.').Skip(1).ToArray());

            return(bindingCollection.Add(property, datasource, skippedName));
        }
Esempio n. 5
0
        public override PropertyDescriptorCollection GetProperties(ITypeDescriptorContext context, object value, Attribute[] attributes)
        {
            if (!(value is ControlBindingsCollection))
            {
                return(new PropertyDescriptorCollection(new PropertyDescriptor[0]));
            }
            ControlBindingsCollection    component  = (ControlBindingsCollection)value;
            PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(component.BindableComponent, (Attribute[])null);
            ArrayList list = new ArrayList();

            for (int i = 0; i < properties.Count; i++)
            {
                Binding binding  = component[properties[i].Name];
                bool    readOnly = (((binding != null) && !(binding.DataSource is IListSource)) && !(binding.DataSource is IList)) && !(binding.DataSource is Array);
                DesignBindingPropertyDescriptor descriptor = new DesignBindingPropertyDescriptor(properties[i], null, readOnly);
                if (((BindableAttribute)properties[i].Attributes[typeof(BindableAttribute)]).Bindable || !((DesignBinding)descriptor.GetValue(component)).IsNull)
                {
                    list.Add(descriptor);
                }
            }
            list.Add(new AdvancedBindingPropertyDescriptor());
            PropertyDescriptor[] array = new PropertyDescriptor[list.Count];
            list.CopyTo(array, 0);
            return(new PropertyDescriptorCollection(array));
        }
Esempio n. 6
0
        public override PropertyDescriptorCollection GetProperties(ITypeDescriptorContext context, object value, Attribute[] attributes)
        {
            if (value is ControlBindingsCollection)
            {
                ControlBindingsCollection collection = (ControlBindingsCollection)value;
                Control control = collection.Control;
                Type    type    = control.GetType();

                PropertyDescriptorCollection bindableProperties = TypeDescriptor.GetProperties(control, null);
                ArrayList props = new ArrayList();
                for (int i = 0; i < bindableProperties.Count; i++)
                {
                    DesignBindingPropertyDescriptor property = new DesignBindingPropertyDescriptor(bindableProperties[i], null);
                    bool bindable = ((BindableAttribute)bindableProperties[i].Attributes[typeof(BindableAttribute)]).Bindable;
                    if (bindable || !((DesignBinding)property.GetValue(collection)).IsNull)
                    {
                        props.Add(property);
                    }
                }

                props.Add(new AdvancedBindingPropertyDescriptor());
                PropertyDescriptor[] propArray = new PropertyDescriptor[props.Count];
                props.CopyTo(propArray, 0);
                return(new PropertyDescriptorCollection(propArray));
            }
            return(new PropertyDescriptorCollection(new PropertyDescriptor[0]));
        }
        public BindableToolStripItem(ToolStripItem toolStripItem)
        {
            _toolStripItem = toolStripItem;

            DataBindings   = new ControlBindingsCollection(this);
            BindingContext = new BindingContext();
        }
Esempio n. 8
0
        public static void Add(ControlBindingsCollection dataBindings, string sP, object tO, string tP, bool formattingEnabled = true, DataSourceUpdateMode change = DataSourceUpdateMode.OnPropertyChanged)
        {
            Binding b = new Binding(sP, tO, tP, formattingEnabled, change);

            b.Format += Binding_Format;
            dataBindings.Add(b);
        }
Esempio n. 9
0
        private static string GetBoundField(Control control, string controlProperty)
        {
            if (control == null)
            {
                throw new ArgumentNullException("control");
            }

            if (controlProperty == null)
            {
                throw new ArgumentNullException("controlProperty");
            }

            ControlBindingsCollection bindings = control.DataBindings;

            if (bindings.Count <= 0)
            {
                throw new InvalidOperationException("There are no bindings for " + control.Name + ".");
            }

            Binding binding = bindings[controlProperty];

            if (binding == null)
            {
                throw new ArgumentException("No binding was set for " + control.Name + "." + controlProperty, "controlProperty");
            }

            return(binding.BindingMemberInfo.BindingField);
        }
Esempio n. 10
0
        public static Binding AddSafeBinding(this ControlBindingsCollection cbc, string propertyName, INotifyPropertyChanged dataSource, string dataMember, bool formattingEnabled, DataSourceUpdateMode dataSourceUpdateMode)
        {
            var threadSafeBinding = ThreadSafeBinding.GetBinding(cbc.Control, propertyName, dataSource, dataMember, formattingEnabled, dataSourceUpdateMode);

            cbc.Add(threadSafeBinding);
            return(threadSafeBinding);
        }
Esempio n. 11
0
        /// <summary>
        /// Extensión para hacer un binding tipado y evitar posibles errores al no depender del string para
        /// las propiedades bindeadas entre el objeto y el source
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="dataBindings"></param>
        /// <param name="dataSource"></param>
        /// <param name="controlLambda"></param>
        /// <param name="objectLambda"></param>
        /// <returns></returns>
        /// <remarks>Objetos sencillos que queramos bindear la propiedad Text</remarks>
        public static Binding Add <T>(this ControlBindingsCollection dataBindings,
                                      object dataSource,
                                      Expression <Func <System.Windows.Forms.Control, object> > controlLambda,
                                      Expression <Func <T, object> > objectLambda)
        {
            string controlPropertyName;
            string bindingTargetName;

            if (controlLambda.Body is MemberExpression)
            {
                controlPropertyName = ((MemberExpression)(controlLambda.Body)).Member.Name;
            }
            else
            {
                var op = ((UnaryExpression)controlLambda.Body).Operand;
                controlPropertyName = ((MemberExpression)op).Member.Name;
            }

            if (objectLambda.Body is MemberExpression)
            {
                bindingTargetName = ((MemberExpression)(objectLambda.Body)).Member.Name;
            }
            else
            {
                var op = ((UnaryExpression)objectLambda.Body).Operand;
                bindingTargetName = ((MemberExpression)op).Member.Name;
            }

            return(dataBindings.Add
                       (controlPropertyName, dataSource, bindingTargetName));
        }
Esempio n. 12
0
        /// <summary>
        /// Extensión para hacer un binding tipado y evitar posibles errores al no depender del string para
        /// las propiedades bindeadas entre el objeto y el source
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="dataBindings"></param>
        /// <param name="dataSource"></param>
        /// <param name="controlLambda"></param>
        /// <param name="objectLambda"></param>
        /// <returns></returns>
        /// <remarks>Para LookUpEdit de DevExpress</remarks>
        public static Binding Add <T>(this ControlBindingsCollection dataBindings,
                                      object dataSource,
                                      Expression <Func <LookUpEdit, object> > controlLambda,
                                      Expression <Func <T, object> > objectLambda)
        {
            string controlPropertyName;
            string bindingTargetName;

            if (controlLambda.Body is MemberExpression)
            {
                controlPropertyName = ((MemberExpression)(controlLambda.Body)).Member.Name;
            }
            else
            {
                var op = ((UnaryExpression)controlLambda.Body).Operand;
                controlPropertyName = ((MemberExpression)op).Member.Name;
            }


            if (objectLambda.Body is MemberExpression)
            {
                bindingTargetName = ((MemberExpression)(objectLambda.Body)).Member.Name;
            }
            else
            {
                var op = ((UnaryExpression)objectLambda.Body).Operand;
                bindingTargetName = ((MemberExpression)op).Member.Name;
            }

            return(dataBindings.Add
                       (controlPropertyName, dataSource, bindingTargetName, true, DataSourceUpdateMode.OnPropertyChanged));
        }
        public void CtorTest()
        {
            BindableToolStripItem     item          = new BindableToolStripItem();
            ControlBindingsCollection data_bindings = new ControlBindingsCollection(item);

            Assert.AreEqual(item, data_bindings.BindableComponent, "#A1");
            Assert.AreEqual(null, data_bindings.Control, "#A2");
        }
Esempio n. 14
0
    public static void Add <T>(this ControlBindingsCollection instance, Expression <Func <T, object> > property)
    {
        var body   = property.Body as UnaryExpression;
        var member = body.Operand as MemberExpression;
        var name   = member.Member.Name;

        instance.Add(name);
    }
        public void Add_AddAlreadyInSameManager_ThrowsArgumentException()
        {
            var control    = new Control();
            var collection = new ControlBindingsCollection(control);
            var binding    = new Binding(null, new object(), "member");

            collection.Add(binding);
            Assert.Throws <ArgumentException>("dataBinding", () => collection.Add(binding));
        }
Esempio n. 16
0
        public static Binding Add <T1, T2>(this ControlBindingsCollection bindingsColection, string propertyName,
                                           T1 dataSource,
                                           Expression <Func <T1, T2> > dataMember, bool formattingEnabled, DataSourceUpdateMode updateMode)
        {
            var expr = (MemberExpression)dataMember.Body;
            var prop = (PropertyInfo)expr.Member;

            return(bindingsColection.Add(propertyName, dataSource, prop.Name, formattingEnabled, updateMode));
        }
        public void Add_Binding_Success(Binding binding)
        {
            var control    = new Control();
            var collection = new ControlBindingsCollection(control);

            collection.Add(binding);
            Assert.Same(binding, Assert.Single(collection));
            Assert.Same(control, binding.BindableComponent);
        }
        public void Add_FormControl_Success()
        {
            var control    = new Form();
            var collection = new ControlBindingsCollection(control);
            var binding    = new Binding(nameof(Control.Text), 1, "dataMember");

            collection.Add(binding);
            Assert.Same(binding, Assert.Single(collection));
        }
        public void Item_PropertyNameExists_ReturnsExpected(string propertyName)
        {
            var control    = new Control();
            var collection = new ControlBindingsCollection(control);
            var binding    = new Binding(nameof(Control.Text), new object(), "member");

            collection.Add(binding);
            Assert.Same(binding, collection[propertyName]);
        }
        public void Item_NoSuchPropertyName_ReturnsNull(string propertyName)
        {
            var control    = new Control();
            var collection = new ControlBindingsCollection(control);
            var binding    = new Binding(nameof(Control.Text), new object(), "member");

            collection.Add(binding);
            Assert.Null(collection[propertyName]);
        }
        public void Ctor_IBindableComponent(IBindableComponent control, Control expectedControl)
        {
            var collection = new ControlBindingsCollection(control);

            Assert.Same(control, collection.BindableComponent);
            Assert.Same(expectedControl, collection.Control);
            Assert.Equal(DataSourceUpdateMode.OnValidation, collection.DefaultDataSourceUpdateMode);
            Assert.Empty(collection);
        }
        private static DesignBinding GetBinding(ControlBindingsCollection bindings, PropertyDescriptor property)
        {
            Binding binding = bindings[property.Name];

            if (binding == null)
            {
                return(DesignBinding.Null);
            }
            return(new DesignBinding(binding.DataSource, binding.BindingMemberInfo.BindingMember));
        }
        public void DefaultDataSourceUpdateMode_Set_GetReturnsExpected(DataSourceUpdateMode value)
        {
            var control    = new Control();
            var collection = new ControlBindingsCollection(control)
            {
                DefaultDataSourceUpdateMode = value
            };

            Assert.Equal(value, collection.DefaultDataSourceUpdateMode);
        }
        public void Remove_NullDataBinding_ThrowsArgumentNullException()
        {
            var control    = new Control();
            var collection = new ControlBindingsCollection(control);
            var binding    = new Binding(null, new object(), "member");

            collection.Add(binding);

            Assert.Throws <ArgumentNullException>("dataBinding", () => collection.Remove(null));
            Assert.Same(binding, Assert.Single(collection));
        }
        public void Add_AlreadyInDifferentManager_ThrowsArgumentException()
        {
            var control1    = new Control();
            var control2    = new Control();
            var collection1 = new ControlBindingsCollection(control1);
            var collection2 = new ControlBindingsCollection(control2);
            var binding     = new Binding(null, new object(), "member");

            collection1.Add(binding);
            Assert.Throws <ArgumentException>("dataBinding", () => collection2.Add(binding));
        }
        public void Remove_NoSuchDataBinding_ThrowsArgumentException()
        {
            using var control = new Control();
            var collection = new ControlBindingsCollection(control);
            var binding1   = new Binding(null, new object(), "member");
            var binding2   = new Binding(null, new object(), "member");

            collection.Add(binding1);

            Assert.Throws <ArgumentException>("dataBinding", () => collection.Remove(binding2));
            Assert.Same(binding1, Assert.Single(collection));
        }
        public void Add_NullDataSource_ThrowsArgumentNullException()
        {
            var control    = new Control();
            var collection = new ControlBindingsCollection(control);

            Assert.Throws <ArgumentNullException>("dataSource", () => collection.Add("propertyName", null, "dataMember"));
            Assert.Throws <ArgumentNullException>("dataSource", () => collection.Add("propertyName", null, "dataMember", true));
            Assert.Throws <ArgumentNullException>("dataSource", () => collection.Add("propertyName", null, "dataMember", true, DataSourceUpdateMode.OnPropertyChanged));
            Assert.Throws <ArgumentNullException>("dataSource", () => collection.Add("propertyName", null, "dataMember", true, DataSourceUpdateMode.OnPropertyChanged, "null"));
            Assert.Throws <ArgumentNullException>("dataSource", () => collection.Add("propertyName", null, "dataMember", true, DataSourceUpdateMode.OnPropertyChanged, "null", "formatString"));
            Assert.Throws <ArgumentNullException>("dataSource", () => collection.Add("propertyName", null, "dataMember", true, DataSourceUpdateMode.OnPropertyChanged, "null", "formatString", CultureInfo.CurrentCulture));
        }
Esempio n. 28
0
        private static void ClearBindings(ControlBindingsCollection b)
        {
            var bindings = new Binding[b.Count];

            b.CopyTo(bindings, 0);
            b.Clear();

            foreach (var binding in bindings)
            {
                TypeDescriptor.Refresh(binding.DataSource);
            }
        }
        public void Add_InvalidBinding_ThrowsArgumentException()
        {
            var control    = new Control();
            var collection = new ControlBindingsCollection(control);

            Assert.Throws <ArgumentException>("PropertyName", () => collection.Add(new Binding("NoSuchProperty", new object(), "dataMember")));
            Assert.Throws <ArgumentException>("PropertyName", () => collection.Add("NoSuchProperty", new object(), "dataMember"));
            Assert.Throws <ArgumentException>("PropertyName", () => collection.Add("NoSuchProperty", new object(), "dataMember", true));
            Assert.Throws <ArgumentException>("PropertyName", () => collection.Add("NoSuchProperty", new object(), "dataMember", true, DataSourceUpdateMode.OnPropertyChanged));
            Assert.Throws <ArgumentException>("PropertyName", () => collection.Add("NoSuchProperty", new object(), "dataMember", true, DataSourceUpdateMode.OnPropertyChanged, "null"));
            Assert.Throws <ArgumentException>("PropertyName", () => collection.Add("NoSuchProperty", new object(), "dataMember", true, DataSourceUpdateMode.OnPropertyChanged, "null", "formatString"));
            Assert.Throws <ArgumentException>("PropertyName", () => collection.Add("NoSuchProperty", new object(), "dataMember", true, DataSourceUpdateMode.OnPropertyChanged, "null", "formatString", CultureInfo.CurrentCulture));
        }
        public static string GetBindingField(this ControlBindingsCollection dataBindings, string property)
        {
            Binding bindingOfInterest = null;

            foreach (Binding binding in dataBindings)
            {
                if (binding.PropertyName == property)
                {
                    bindingOfInterest = binding;
                }
            }

            return(bindingOfInterest?.BindingMemberInfo.BindingField ?? null);
        }