示例#1
0
        public void ConvertFromObjectTest()
        {
            var converter = new CalcConverter();
            Assert.AreEqual(10, converter.ConvertBack((object)15, typeof(int), "{0}+5", CultureInfo.CurrentCulture));

            converter = new CalcConverter();
            Assert.AreEqual(9.2, converter.ConvertBack((object)15.0, typeof(double), "{0}+5.8", CultureInfo.CurrentCulture));

            converter = new CalcConverter();
            Assert.AreEqual(true, converter.ConvertBack((object)false, typeof(bool), "!{0}", CultureInfo.CurrentCulture));
        }
示例#2
0
        public void ConvertFromStringTest()
        {
            var converter = new CalcConverter();
            Assert.AreEqual(10, converter.ConvertBack("15", typeof(int), "{0}+5", CultureInfo.CurrentCulture));

            converter = new CalcConverter();
            Assert.AreEqual(10.299999999999999, converter.ConvertBack("15.7", typeof(double), "{0}+5.4", CultureInfo.CurrentCulture));

            converter = new CalcConverter();
            Assert.AreEqual(false, converter.ConvertBack("True", typeof(bool), "!{0}", CultureInfo.CurrentCulture));
        }
示例#3
0
        public void ConvertToStringTest()
        {
            var converter = new CalcConverter();
            Assert.AreEqual("15", converter.Convert(10, typeof(string), "{0}+(double)5", CultureInfo.CurrentCulture));

            converter = new CalcConverter();
            Assert.AreEqual("15.3", converter.Convert(10, typeof(string), "{0}+5.3", CultureInfo.CurrentCulture));

            converter = new CalcConverter();
            Assert.AreEqual("False", converter.Convert(false, typeof(string), "{0}&&true", CultureInfo.CurrentCulture));
        }
示例#4
0
 public void ConvertToObjectTest()
 {
     var converter = new CalcConverter();
     Assert.AreEqual(15, converter.Convert(10, typeof(object), "{0}+5", CultureInfo.CurrentCulture));
     
     converter = new CalcConverter();
     Assert.AreEqual(15.3, converter.Convert(10, typeof(object), "{0}+5.3", CultureInfo.CurrentCulture));
     
     converter = new CalcConverter();
     Assert.AreEqual(true, converter.Convert(true, typeof(object), "{0}&&true", CultureInfo.CurrentCulture));
 }
        public void ConvertBoolToVisibilityTest()
        {
            var converter = new CalcConverter();

            Assert.AreEqual(Visibility.Visible, 
                new BoolToVisibilityConverter()
                .Convert(true, typeof(Visibility), null, CultureInfo.CurrentCulture));
            
            Assert.AreEqual(Visibility.Collapsed, 
                new BoolToVisibilityConverter()
                .Convert(false, typeof(Visibility), null, CultureInfo.CurrentCulture));

            Assert.AreEqual(Visibility.Hidden, 
                new BoolToVisibilityConverter(FalseToVisibility.Hidden)
                .Convert(false, typeof(Visibility), null, CultureInfo.CurrentCulture));
        }
示例#6
0
        public override object ProvideValue(IServiceProvider serviceProvider)
        {
            var targetPropertyType = GetPropertyType(serviceProvider);
            var typeResolver       = (IXamlTypeResolver)serviceProvider.GetService(typeof(IXamlTypeResolver));
            var typeDescriptor     = serviceProvider as ITypeDescriptorContext;

            var normalizedPath = NormalizePath(Path);
            var pathes         = GetSourcePathes(normalizedPath, typeResolver);

            var expressionTemplate = GetExpressionTemplate(normalizedPath, pathes, out Dictionary <string, Type> enumParameters);

            var mathConverter = new CalcConverter(_parser.Value, enumParameters)
            {
                FalseToVisibility   = FalseToVisibility,
                StringFormatDefined = StringFormat != null,
            };

            var bindingPathes = pathes
                                .Where(p => p.PathId.PathType == PathTokenType.Property ||
                                       p.PathId.PathType == PathTokenType.StaticProperty).ToList();

            BindingBase resBinding;

            if (bindingPathes.Count == 1)
            {
                // todo: can enums be binded ? What if one value is Enum? bug..
                var binding = new System.Windows.Data.Binding()
                {
                    Mode = Mode,
                    NotifyOnSourceUpdated       = NotifyOnSourceUpdated,
                    NotifyOnTargetUpdated       = NotifyOnTargetUpdated,
                    NotifyOnValidationError     = NotifyOnValidationError,
                    UpdateSourceExceptionFilter = UpdateSourceExceptionFilter,
                    UpdateSourceTrigger         = UpdateSourceTrigger,
                    ValidatesOnDataErrors       = ValidatesOnDataErrors,
                    ValidatesOnExceptions       = ValidatesOnExceptions,
#if NET45
                    ValidatesOnNotifyDataErrors = ValidatesOnNotifyDataErrors,
#endif
                };

                var pathId = bindingPathes.Single().PathId;
                // we need to use convert from string for support of static properties
                var pathValue = pathId.Value;

                if (pathId.PathType == PathTokenType.StaticProperty)
                {
                    pathValue = string.Format("({0})", pathValue);  // need to use brackets for Static property recognition in standart binding
                }
                var resPath = (PropertyPath) new PropertyPathConverter().ConvertFromString(typeDescriptor, pathValue);
                binding.Path = resPath;

                if (Source != null)
                {
                    binding.Source = Source;
                }

                if (ElementName != null)
                {
                    binding.ElementName = ElementName;
                }

                if (RelativeSource != null)
                {
                    binding.RelativeSource = RelativeSource;
                }

                if (StringFormat != null)
                {
                    binding.StringFormat = StringFormat;
                }

                // we don't use converter if binding is trivial - {0}, except type convertion from bool to visibility

                //todo: use more smart recognition for template (with analysing brackets ({1}) any count )
                // trivial binding, CalcBinding converter is not needed
                if ((expressionTemplate != "{0}" && expressionTemplate != "({0})") || targetPropertyType == typeof(Visibility))
                {
                    binding.Converter          = mathConverter;
                    binding.ConverterParameter = expressionTemplate;
                    binding.ConverterCulture   = ConverterCulture;
                }
                resBinding = binding;
            }
            else
            {
                var mBinding = new MultiBinding
                {
                    Converter          = mathConverter,
                    ConverterParameter = expressionTemplate,
                    ConverterCulture   = ConverterCulture,
                    Mode = BindingMode.OneWay,
                    NotifyOnSourceUpdated       = NotifyOnSourceUpdated,
                    NotifyOnTargetUpdated       = NotifyOnTargetUpdated,
                    NotifyOnValidationError     = NotifyOnValidationError,
                    UpdateSourceExceptionFilter = UpdateSourceExceptionFilter,
                    UpdateSourceTrigger         = UpdateSourceTrigger,
                    ValidatesOnDataErrors       = ValidatesOnDataErrors,
                    ValidatesOnExceptions       = ValidatesOnExceptions,
#if NET45
                    ValidatesOnNotifyDataErrors = ValidatesOnNotifyDataErrors,
#endif
                };

                if (StringFormat != null)
                {
                    mBinding.StringFormat = StringFormat;
                }

                foreach (var path in bindingPathes)
                {
                    var binding = new System.Windows.Data.Binding();

                    // we need to use convert from string for support of static properties
                    var pathValue = path.PathId.Value;

                    if (path.PathId.PathType == PathTokenType.StaticProperty)
                    {
                        pathValue = string.Format("({0})", pathValue);  // need to use brackets for Static property recognition in standart binding
                    }

                    var resPath = (PropertyPath) new PropertyPathConverter().ConvertFromString(typeDescriptor, pathValue);

                    binding.Path = resPath;

                    if (Source != null)
                    {
                        binding.Source = Source;
                    }

                    if (ElementName != null)
                    {
                        binding.ElementName = ElementName;
                    }

                    if (RelativeSource != null)
                    {
                        binding.RelativeSource = RelativeSource;
                    }

                    mBinding.Bindings.Add(binding);
                }

                resBinding = mBinding;
            }

            return(resBinding.ProvideValue(serviceProvider));
        }
示例#7
0
        public override object ProvideValue(IServiceProvider serviceProvider)
        {
            var targetPropertyType = GetPropertyType(serviceProvider);
            var normalizedPath     = NormalizePath(Path);
            var sourcePropertiesPathesWithPositions = GetSourcePropertiesPathes(normalizedPath);
            var expressionTemplate = GetExpressionTemplate(normalizedPath, sourcePropertiesPathesWithPositions);

            var mathConverter = new CalcConverter
            {
                FalseToVisibility   = FalseToVisibility,
                StringFormatDefined = StringFormat != null
            };

            BindingBase resBinding;

            if (sourcePropertiesPathesWithPositions.Count() == 1)
            {
                var binding = new System.Windows.Data.Binding(sourcePropertiesPathesWithPositions.Single().Item1)
                {
                    Mode = Mode,
                    NotifyOnSourceUpdated       = NotifyOnSourceUpdated,
                    NotifyOnTargetUpdated       = NotifyOnTargetUpdated,
                    NotifyOnValidationError     = NotifyOnValidationError,
                    UpdateSourceExceptionFilter = UpdateSourceExceptionFilter,
                    UpdateSourceTrigger         = UpdateSourceTrigger,
                    ValidatesOnDataErrors       = ValidatesOnDataErrors,
                    ValidatesOnExceptions       = ValidatesOnExceptions,
#if NET45
                    ValidatesOnNotifyDataErrors = ValidatesOnNotifyDataErrors,
#endif
                };

                if (Source != null)
                {
                    binding.Source = Source;
                }

                if (ElementName != null)
                {
                    binding.ElementName = ElementName;
                }

                if (RelativeSource != null)
                {
                    binding.RelativeSource = RelativeSource;
                }

                if (StringFormat != null)
                {
                    binding.StringFormat = StringFormat;
                }

                // we don't use converter if binding is trivial - {0}, except type convertion from bool to visibility
                if (expressionTemplate != "{0}" || targetPropertyType == typeof(Visibility))
                {
                    binding.Converter          = mathConverter;
                    binding.ConverterParameter = expressionTemplate;
                    binding.ConverterCulture   = ConverterCulture;
                }
                resBinding = binding;
            }
            else
            {
                var mBinding = new MultiBinding
                {
                    Converter          = mathConverter,
                    ConverterParameter = expressionTemplate,
                    ConverterCulture   = ConverterCulture,
                    Mode = BindingMode.OneWay,
                    NotifyOnSourceUpdated       = NotifyOnSourceUpdated,
                    NotifyOnTargetUpdated       = NotifyOnTargetUpdated,
                    NotifyOnValidationError     = NotifyOnValidationError,
                    UpdateSourceExceptionFilter = UpdateSourceExceptionFilter,
                    UpdateSourceTrigger         = UpdateSourceTrigger,
                    ValidatesOnDataErrors       = ValidatesOnDataErrors,
                    ValidatesOnExceptions       = ValidatesOnExceptions,
#if NET45
                    ValidatesOnNotifyDataErrors = ValidatesOnNotifyDataErrors,
#endif
                };

                if (StringFormat != null)
                {
                    mBinding.StringFormat = StringFormat;
                }

                foreach (var sourcePropertyPathWithPositions in sourcePropertiesPathesWithPositions)
                {
                    var binding = new System.Windows.Data.Binding(sourcePropertyPathWithPositions.Item1);

                    if (Source != null)
                    {
                        binding.Source = Source;
                    }

                    if (ElementName != null)
                    {
                        binding.ElementName = ElementName;
                    }

                    if (RelativeSource != null)
                    {
                        binding.RelativeSource = RelativeSource;
                    }

                    mBinding.Bindings.Add(binding);
                }

                resBinding = mBinding;
            }

            return(resBinding.ProvideValue(serviceProvider));
        }
示例#8
0
        public void BindingBackCompileTimesShouldBeTwoTest()
        {
            var interpreterMock = new CompileTimesMockInterpreterParser();
            var converter = new CalcConverter(interpreterMock);

            converter.ConvertBack("15", typeof(int), "{0}+5", CultureInfo.CurrentCulture);

            Assert.AreEqual(2, interpreterMock.ParseCalls.Count);
            Assert.AreEqual(1, interpreterMock.ParseCalls.First().Value);
            Assert.AreEqual(1, interpreterMock.ParseCalls.Skip(1).First().Value);
        }
示例#9
0
        public void BindingCompileTimesShouldBeOneTest()
        {
            var interpreterMock = new CompileTimesMockInterpreterParser();
            var converter = new CalcConverter(interpreterMock);

            converter.Convert("15", typeof(string), "{0}+\"5\"", CultureInfo.CurrentCulture);

            Assert.AreEqual(1, interpreterMock.ParseCalls.Count);
            Assert.AreEqual(1, interpreterMock.ParseCalls.First().Value);
        }
示例#10
0
 public void NullableValueTypesAreNotSupportedInConvertBackBinding()
 {
     var converter = new CalcConverter();
     // instead of NULL there must be //5// but nullable values doesn't support so binding retrieve NULL instead of //5//
     Assert.AreEqual(null, converter.ConvertBack((object)10.0, typeof(double?), "{0}+5", CultureInfo.CurrentCulture));        
 }
示例#11
0
        public override object ProvideValue(IServiceProvider serviceProvider)
        {
            var targetPropertyType = GetPropertyType(serviceProvider);
            var normalizedPath = NormalizePath(Path);
            var sourcePropertiesPathesWithPositions = GetSourcePropertiesPathes(normalizedPath);
            var expressionTemplate = GetExpressionTemplate(normalizedPath, sourcePropertiesPathesWithPositions);

            var mathConverter = new CalcConverter
            {
                FalseToVisibility = FalseToVisibility,
                StringFormatDefined = StringFormat != null
            };

            BindingBase resBinding;

            if (sourcePropertiesPathesWithPositions.Count() == 1)
            {
                var binding = new System.Windows.Data.Binding(sourcePropertiesPathesWithPositions.Single().Item1)
                {
                    Mode = Mode,
                    NotifyOnSourceUpdated = NotifyOnSourceUpdated,
                    NotifyOnTargetUpdated = NotifyOnTargetUpdated,
                    NotifyOnValidationError = NotifyOnValidationError,
                    UpdateSourceExceptionFilter = UpdateSourceExceptionFilter,
                    UpdateSourceTrigger = UpdateSourceTrigger,
                    ValidatesOnDataErrors = ValidatesOnDataErrors,
                    ValidatesOnExceptions = ValidatesOnExceptions,
#if NET45
                    ValidatesOnNotifyDataErrors = ValidatesOnNotifyDataErrors,
#endif
                };

                if (Source != null)
                    binding.Source = Source;

                if (ElementName != null)
                    binding.ElementName = ElementName;

                if (RelativeSource != null)
                    binding.RelativeSource = RelativeSource;

                if (StringFormat != null)
                    binding.StringFormat = StringFormat;

                // we don't use converter if binding is trivial - {0}, except type convertion from bool to visibility
                if (expressionTemplate != "{0}" || targetPropertyType == typeof(Visibility))
                {
                    binding.Converter = mathConverter;
                    binding.ConverterParameter = expressionTemplate;
                    binding.ConverterCulture = ConverterCulture;
                }
                resBinding = binding;
            }
            else
            {
                var mBinding = new MultiBinding
                {
                    Converter = mathConverter,
                    ConverterParameter = expressionTemplate,
                    ConverterCulture = ConverterCulture,
                    Mode = BindingMode.OneWay,
                    NotifyOnSourceUpdated = NotifyOnSourceUpdated,
                    NotifyOnTargetUpdated = NotifyOnTargetUpdated,
                    NotifyOnValidationError = NotifyOnValidationError,
                    UpdateSourceExceptionFilter = UpdateSourceExceptionFilter,
                    UpdateSourceTrigger = UpdateSourceTrigger,
                    ValidatesOnDataErrors = ValidatesOnDataErrors,
                    ValidatesOnExceptions = ValidatesOnExceptions,
#if NET45
                    ValidatesOnNotifyDataErrors = ValidatesOnNotifyDataErrors,
#endif
                };

                if (StringFormat != null)
                    mBinding.StringFormat = StringFormat;

                foreach (var sourcePropertyPathWithPositions in sourcePropertiesPathesWithPositions)
                {
                    var binding = new System.Windows.Data.Binding(sourcePropertyPathWithPositions.Item1);

                    if (Source != null)
                        binding.Source = Source;

                    if (ElementName != null)
                        binding.ElementName = ElementName;

                    if (RelativeSource != null)
                        binding.RelativeSource = RelativeSource;

                    mBinding.Bindings.Add(binding);
                }

                resBinding = mBinding;
            }
            
            return resBinding.ProvideValue(serviceProvider);
        }