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)); }
public void Add_NullDataBinding_ThrowsArgumentNullException() { var control = new Control(); var collection = new ControlBindingsCollection(control); Assert.Throws <ArgumentNullException>("dataBinding", () => collection.Add(null)); }
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); }
/// <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)); }
/// <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 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 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 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 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_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 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_ControlBindings_Success() { var control = new SubControl(); ControlBindingsCollection collection = control.DataBindings; var binding1 = new Binding(nameof(SubControl.Text), 1, "dataMember"); var binding2 = new Binding(nameof(SubControl.AccessibleRole), 1, "dataMember"); var binding3 = new Binding(null, 1, "dataMember"); var binding4 = new Binding("", 1, "dataMember"); var binding5 = new Binding(nameof(SubControl.text), 1, "dataMember"); collection.Add(binding1); collection.Add(binding2); collection.Add(binding3); collection.Add(binding4); collection.Add(binding5); Assert.Equal(5, collection.Count); Assert.Same(binding1, collection[0]); Assert.Same(binding2, collection[1]); Assert.Same(binding3, collection[2]); Assert.Same(binding4, collection[3]); Assert.Same(binding5, collection[4]); }
public void Add_DuplicateBinding_ThrowsArgumentException() { var control = new Control(); ControlBindingsCollection collection = control.DataBindings; var binding = new Binding(nameof(Control.Text), new object(), "dataMember"); collection.Add(binding); Assert.Throws <ArgumentException>("binding", () => collection.Add(new Binding(nameof(Control.Text), new object(), "dataMember"))); Assert.Throws <ArgumentException>("binding", () => collection.Add(nameof(Control.Text), new object(), "dataMember")); Assert.Throws <ArgumentException>("binding", () => collection.Add(nameof(Control.Text), new object(), "dataMember", true)); Assert.Throws <ArgumentException>("binding", () => collection.Add(nameof(Control.Text), new object(), "dataMember", true, DataSourceUpdateMode.OnPropertyChanged)); Assert.Throws <ArgumentException>("binding", () => collection.Add(nameof(Control.Text), new object(), "dataMember", true, DataSourceUpdateMode.OnPropertyChanged, "null")); Assert.Throws <ArgumentException>("binding", () => collection.Add(nameof(Control.Text), new object(), "dataMember", true, DataSourceUpdateMode.OnPropertyChanged, "null", "formatString")); Assert.Throws <ArgumentException>("binding", () => collection.Add(nameof(Control.Text), new object(), "dataMember", true, DataSourceUpdateMode.OnPropertyChanged, "null", "formatString", CultureInfo.CurrentCulture)); }
public void RemoveAt_Invoke_Success() { var control = new Control(); var collection = new ControlBindingsCollection(control); var binding = new Binding(null, new object(), "member"); collection.Add(binding); Assert.Same(binding, Assert.Single(collection)); Assert.Same(control, binding.BindableComponent); collection.RemoveAt(0); Assert.Empty(collection); Assert.Null(binding.BindableComponent); }
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)); }
private static void SetBinding(ControlBindingsCollection bindings, PropertyDescriptor property, DesignBinding designBinding) { if (designBinding != null) { Binding binding = bindings[property.Name]; if (binding != null) { bindings.Remove(binding); } if (!designBinding.IsNull) { bindings.Add(property.Name, designBinding.DataSource, designBinding.DataMember); } } }
public void Add_StringObjectStringBoolDataSourceUpdateModeObjectStringIFormatProvider_Success() { var control = new Control(); var collection = new ControlBindingsCollection(control); Binding binding = collection.Add(nameof(Control.Text), 1, "dataMember", true, DataSourceUpdateMode.OnPropertyChanged, "null", "formatString", CultureInfo.CurrentCulture); Assert.Same(binding, Assert.Single(collection)); Assert.Same(control, binding.BindableComponent); Assert.Equal(nameof(Control.Text), binding.PropertyName); Assert.Equal(1, binding.DataSource); Assert.True(binding.FormattingEnabled); Assert.Equal(DataSourceUpdateMode.OnPropertyChanged, binding.DataSourceUpdateMode); Assert.Equal("null", binding.NullValue); Assert.Equal("formatString", binding.FormatString); Assert.Equal(CultureInfo.CurrentCulture, binding.FormatInfo); }
public void Add_StringObjectStringBool_Success() { var control = new Control(); var collection = new ControlBindingsCollection(control); Binding binding = collection.Add(nameof(Control.Text), 1, "dataMember", true); Assert.Same(binding, Assert.Single(collection)); Assert.Same(control, binding.BindableComponent); Assert.Equal(nameof(Control.Text), binding.PropertyName); Assert.Equal(1, binding.DataSource); Assert.True(binding.FormattingEnabled); Assert.Equal(DataSourceUpdateMode.OnValidation, binding.DataSourceUpdateMode); Assert.Null(binding.NullValue); Assert.Empty(binding.FormatString); Assert.Null(binding.FormatInfo); }
public void Remove_DataBindingFromOtherCollection_ThrowsArgumentException() { var control1 = new Control(); var control2 = new Control(); var collection1 = new ControlBindingsCollection(control1); var collection2 = new ControlBindingsCollection(control2); var binding1 = new Binding(null, new object(), "member"); var binding2 = new Binding(null, new object(), "member"); collection1.Add(binding1); collection2.Add(binding2); Assert.Throws <ArgumentException>("dataBinding", () => collection2.Remove(binding1)); Assert.Same(binding1, Assert.Single(collection1)); Assert.Same(binding2, Assert.Single(collection2)); }
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)); }