public IList <IBindingBuilder> CreateBuildersFromString(object target, string bindingExpression, IList <object> sources = null, IDataContext context = null) { Should.NotBeNull(target, nameof(target)); Should.NotBeNull(bindingExpression, nameof(bindingExpression)); try { var parserResult = Parser.Parse(target, bindingExpression, sources, context); var result = new IBindingBuilder[parserResult.Count]; for (int index = 0; index < parserResult.Count; index++) { var builder = new BindingBuilder(parserResult[index]); builder.Add(BindingBuilderConstants.BuildDelegate, _buildDelegate); result[index] = builder; } return(result); } catch (Exception exception) { exception = new InvalidOperationException(exception.Message, exception); var builder = new BindingBuilder(); builder.Add(BindingBuilderConstants.Target, target); builder.Add(ProviderConstant, this); builder.Add(ExceptionConstant, exception); builder.Add(BindingBuilderConstants.BuildDelegate, CreateInvalidaDataBindingDelegate); return(new IBindingBuilder[] { builder }); } }
/// <summary> /// Creates a series of instances of <see cref="IBindingBuilder" />. /// </summary> /// <param name="target">The specified binding target.</param> /// <param name="bindingExpression">The specified binding expression.</param> /// <param name="sources">The specified sources, if any.</param> /// <returns>An instance of <see cref="IBindingBuilder" />.</returns> public IList <IBindingBuilder> CreateBuildersFromString(object target, string bindingExpression, IList <object> sources = null) { Should.NotBeNull(target, "target"); Should.NotBeNullOrWhitespace(bindingExpression, "bindingExpression"); try { var parserResult = Parser.Parse(bindingExpression, sources.IsNullOrEmpty() ? DataContext.Empty : new DataContext(1) { { BindingBuilderConstants.RawSources, sources } }); var result = new IBindingBuilder[parserResult.Count]; for (int index = 0; index < parserResult.Count; index++) { var builder = new BindingBuilder(parserResult[index]); builder.Add(BindingBuilderConstants.Target, target); builder.Add(BindingBuilderConstants.BuildDelegate, _buildDelegate); result[index] = builder; } return(result); } catch (Exception exception) { exception = new InvalidOperationException(exception.Message, exception); var builder = new BindingBuilder(); builder.Add(BindingBuilderConstants.Target, target); builder.Add(ProviderConstant, this); builder.Add(ExceptionConstant, exception); builder.Add(BindingBuilderConstants.BuildDelegate, CreateInvalidaDataBindingDelegate); return(new IBindingBuilder[] { builder }); } }
/// <summary> /// Creates an instance of <see cref="IBindingBuilder" />. /// </summary> /// <param name="context">The specified context.</param> /// <returns>An instance of <see cref="IBindingBuilder" />.</returns> public IBindingBuilder CreateBuilder(IDataContext context) { Should.NotBeNull(context, "context"); if (context.Contains(BindingBuilderConstants.BuildDelegate)) { return(new BindingBuilder(context)); } if (context.IsReadOnly) { context = new DataContext(context); } var builder = new BindingBuilder(context); builder.Add(BindingBuilderConstants.BuildDelegate, _buildDelegate); return(builder); }
public void BuilderShouldUseEventArgs3() { var builder = new BindingBuilder(); builder.Bind(new object(), "empty").To<BindingSourceModel>(() => model => BindingSyntaxEx.EventArgs<BindingSourceModel>().ObjectProperty); var sourceModel = new BindingSourceModel { ObjectProperty = "test" }; builder.Add(BindingConstants.CurrentEventArgs, sourceModel); var expression = builder.GetData(BindingBuilderConstants.MultiExpression); expression(builder, Empty.Array<object>()).ShouldEqual(sourceModel.ObjectProperty); }
public void BuilderShouldUseEventArgs2() { var builder = new BindingBuilder(); builder.Bind(new object(), "empty").To<BindingSourceModel>(() => model => BindingSyntaxEx.EventArgs<EventArgs>().GetType().Name); builder.Add(BindingConstants.CurrentEventArgs, EventArgs.Empty); var expression = builder.GetData(BindingBuilderConstants.MultiExpression); expression(builder, Empty.Array<object>()).ShouldEqual(EventArgs.Empty.GetType().Name); }
public void BuilderShouldUseEventArgsForParameter2() { var builder = new BindingBuilder(); builder.Bind(new object(), "empty").To<BindingSourceModel>(() => m => m.IntProperty) .WithCommandParameter(() => model => BindingSyntaxEx.EventArgs<EventArgs>().GetType().Name); builder.Add(BindingConstants.CurrentEventArgs, EventArgs.Empty); var expression = builder.GetData(BindingBuilderConstants.CommandParameter); expression(builder).ShouldEqual(EventArgs.Empty.GetType().Name); }
public void ParserShouldParseMultiExpressionWithConverter() { const string targetPath = "Text"; const string sourcePath1 = "SourceText1"; const string sourcePath2 = "SourceText2"; const string converterName = "TestConverter"; const string binding = "Text $TestConverter(SourceText1, SourceText2)"; var target = new object(); var args = new object[] { "tset", 1 }; var sourceModel = new BindingSourceModel { IntProperty = int.MaxValue }; var bindingMock = new DataBindingMock { TargetAccessor = new BindingSourceAccessorMock { Source = new BindingSourceMock { GetPathMembers = b => new BindingPathMembersMock(this, BindingPath.Create("IntProperty"), new BindingMemberInfo("IntProperty", BindingSourceModel.IntPropertyInfo, typeof(BindingSourceModel))) } } }; var converterMock = new ValueConverterCoreMock { Convert = (o, type, arg3, arg4) => { type.ShouldEqual(BindingSourceModel.IntPropertyInfo.PropertyType); o.ShouldEqual(args[0]); arg3.ShouldEqual(args[1]); return sourceModel; } }; var provider = new BindingProvider(); var resolver = new BindingResourceResolver(); BindingServiceProvider.ResourceResolver = resolver; resolver.AddConverter(converterName, converterMock, true); IBindingParser bindingParser = CreateBindingParser(bindingProvider: provider); var context = new BindingBuilder(bindingParser.Parse(binding, EmptyContext, target, null).Single()); context.Add(BindingConstants.Binding, bindingMock); IBindingPath path = context.GetData(BindingBuilderConstants.TargetPath); path.Path.ShouldEqual(targetPath); var expression = context.GetData(BindingBuilderConstants.MultiExpression); expression(context, args).ShouldEqual(sourceModel); var sources = context.GetData(BindingBuilderConstants.Sources); BindingSourceShouldBeValidDataContext(target, sources[0].Invoke(context), sourcePath1); BindingSourceShouldBeValidDataContext(target, sources[1].Invoke(context), sourcePath2); }
public void ParserShouldParseExpressionWithEventArgs() { const string targetPath = "Text"; const string binding = @"Text $args"; IBindingParser bindingParser = CreateBindingParser(); var context = new BindingBuilder(bindingParser.Parse(binding, EmptyContext, new object(), null).Single()); var path = context.GetData(BindingBuilderConstants.TargetPath); path.Path.ShouldEqual(targetPath); context.Add(BindingConstants.CurrentEventArgs, EventArgs.Empty); var expression = context.GetData(BindingBuilderConstants.MultiExpression); expression(context, Empty.Array<object>()).ShouldEqual(EventArgs.Empty); }
public IList<IBindingBuilder> CreateBuildersFromString(object target, string bindingExpression, IList<object> sources = null) { Should.NotBeNull(target, "target"); Should.NotBeNullOrWhitespace(bindingExpression, "bindingExpression"); try { var parserResult = Parser.Parse(bindingExpression, DataContext.Empty, target, sources); var result = new IBindingBuilder[parserResult.Count]; for (int index = 0; index < parserResult.Count; index++) { var builder = new BindingBuilder(parserResult[index]); builder.Add(BindingBuilderConstants.BuildDelegate, _buildDelegate); result[index] = builder; } return result; } catch (Exception exception) { exception = new InvalidOperationException(exception.Message, exception); var builder = new BindingBuilder(); builder.Add(BindingBuilderConstants.Target, target); builder.Add(ProviderConstant, this); builder.Add(ExceptionConstant, exception); builder.Add(BindingBuilderConstants.BuildDelegate, CreateInvalidaDataBindingDelegate); return new IBindingBuilder[] { builder }; } }
public void ParserShouldParseExpressionWithBinding() { const string targetPath = "Text"; const string binding = @"Text $binding"; var dataContext = new DataContext(); var bindingMock = new DataBindingMock { GetContext = () => dataContext }; IBindingParser bindingParser = CreateBindingParser(); var context = new BindingBuilder(bindingParser.Parse(new object(), binding, null, null).Single()); var path = context.GetData(BindingBuilderConstants.TargetPath); path.Path.ShouldEqual(targetPath); context.Add(BindingConstants.Binding, bindingMock); var expression = context.GetData(BindingBuilderConstants.MultiExpression); expression(context, Empty.Array<object>()).ShouldEqual(bindingMock); }