Inheritance: Freezable
Ejemplo n.º 1
0
        public static BindingProxy GetValueProxy(IResourceContext context, string propertyKey)
        {
            var key = new BindingProxyKey(propertyKey);

            if (context.TryFindResource(key) is BindingProxy proxy)
            {
                return(proxy);
            }

            proxy = new BindingProxy();
            context.AddResource(key, proxy);
            return(proxy);
        }
Ejemplo n.º 2
0
        public void BindingProxyTest()
        {
            var testBindingProxy = new BindingProxy
            {
                Data = "test"
            };

            // for code coverage
            testBindingProxy.Clone();

            Assert.AreEqual(testBindingProxy.Data, "test");
            Assert.IsInstanceOfType(testBindingProxy.As <string>(), typeof(string));
        }
        public static BindingProxy GetValue(this IValueProvider valueProvider, IResourceContext context)
        {
            var proxy = new BindingProxy();
            var value = valueProvider.ProvideValue(context);

            if (value is BindingBase binding)
            {
                BindingOperations.SetBinding(proxy, BindingProxy.ValueProperty, binding);
            }
            else
            {
                proxy.Value = value;
            }

            return(proxy);
        }
Ejemplo n.º 4
0
 /// <summary>
 /// Сохранение атрибутов пользователя в базе
 /// </summary>
 public void Save(object parameter = null)
 {
     try
     {
         BindingProxy bindProxy = parameter as BindingProxy;
         PgUserM      pgUser    = bindProxy.Data as PgUserM;
         int          id        = Source.InsertUpdateUser(pgUser);
         if (pgUser.IsNewUser)
         {
             pgUser         = Source.Users.First(u => u.ID == id);
             bindProxy.Data = pgUser;
         }
     }
     catch (Exception ex)
     {
         Classes.workLogFile.writeLogFile(ex, true, true);
     }
 }
Ejemplo n.º 5
0
        /// <summary>
        /// Returns a <see cref="BindingProxy"/> bound to the value returned by <see cref="ProvideValue"/>.
        /// </summary>
        /// <param name="name">Resource name. This is not the object property name.</param>
        /// <returns></returns>
        public BindingProxy this[string name]
        {
            get
            {
                if (proxyCache.TryGetValue(name, out var proxy))
                {
                    return(proxy);
                }

                proxy            = new BindingProxy();
                proxyCache[name] = proxy;
                var value = ProvideValue(name);
                if (value is BindingBase binding)
                {
                    BindingOperations.SetBinding(proxy, BindingProxy.ValueProperty, binding);
                }
                else
                {
                    proxy.Value = value;
                }

                return(proxy);
            }
        }
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            try
            {
                BindingProxy proxy = parameter as BindingProxy;

                AppResEditorViewModel model = proxy.Data as AppResEditorViewModel;

                if (model.ResourceType.Equals("Text"))
                {
                    return(model.TextList);
                }
                if (model.ResourceType.Equals("Color"))
                {
                    return(model.ColorList);
                }

                return(DependencyProperty.UnsetValue);
            }
            catch (Exception e)
            {
                throw e;
            }
        }
        private static IValidatorProvider CreateValidator(Type modelType, string propertyKey, ValueAttribute attribute, Func <string, object> deserializer)
        {
            Func <IResourceContext, IProxy> argumentProvider;
            var argument = attribute.Argument;

            if (argument is string expression)
            {
                var boundExpression = BoundExpression.Parse(expression);
                if (boundExpression.IsPlainString)
                {
                    var literal = new PlainObject(deserializer != null
                        ? deserializer(boundExpression.StringFormat)
                        : boundExpression.StringFormat);

                    argumentProvider = context => literal;
                }
                else
                {
                    var getString = boundExpression.StringFormat != null;
                    var action    = attribute.ArgumentUpdatedAction;
                    var notify    = action == ValidationAction.ClearErrors || action == ValidationAction.ValidateField;
                    argumentProvider = context =>
                    {
                        var value = getString
                            ? (IProxy)boundExpression.GetStringValue(context)
                            : boundExpression.GetValue(context);

                        if (notify)
                        {
                            value.ValueChanged = () =>
                            {
                                object model;
                                try
                                {
                                    model = context.GetModelInstance();
                                }
                                catch
                                {
                                    // Something went wrong so it's best to
                                    // disable the feature entirely.
                                    value.ValueChanged = null;
                                    return;
                                }

                                if (model is ExpandoObject && modelType == null)
                                {
                                    // Do nothing.
                                }
                                else if (model == null || model.GetType() != modelType)
                                {
                                    // Self dispose when form indicates model change.
                                    value.ValueChanged = null;
                                    return;
                                }

                                if (action == ValidationAction.ValidateField)
                                {
                                    ModelState.Validate(model, propertyKey);
                                }
                                else
                                {
                                    ModelState.ClearValidationErrors(model, propertyKey);
                                }
                            };
                        }

                        return(value);
                    };
                }
            }
            else
            {
                var literal = new PlainObject(argument);
                argumentProvider = context => literal;
            }

            BindingProxy ValueProvider(IResourceContext context)
            {
                var key = new BindingProxyKey(propertyKey);

                if (context.TryFindResource(key) is BindingProxy proxy)
                {
                    return(proxy);
                }

                proxy = new BindingProxy();
                context.AddResource(key, proxy);
                return(proxy);
            }

            Func <IResourceContext, IBoolProxy> isEnforcedProvider;

            switch (attribute.When)
            {
            case null:
                isEnforcedProvider = context => new PlainBool(true);
                break;

            case string expr:
                var boundExpression = BoundExpression.Parse(expr);
                if (!boundExpression.IsSingleResource)
                {
                    throw new ArgumentException(
                              "The provided value must be a bound resource or a literal bool value.", nameof(attribute));
                }

                isEnforcedProvider = context => boundExpression.GetBoolValue(context);
                break;

            case bool b:
                isEnforcedProvider = context => new PlainBool(b);
                break;

            default:
                throw new ArgumentException(
                          "The provided value must be a bound resource or a literal bool value.", nameof(attribute));
            }

            Func <IResourceContext, IErrorStringProvider> errorProvider;
            var message = attribute.Message;

            if (message == null)
            {
                switch (attribute.Condition)
                {
                case Must.BeGreaterThan:
                    message = "Value must be greater than {Argument}.";
                    break;

                case Must.BeGreaterThanOrEqualTo:
                    message = "Value must be greater than or equal to {Argument}.";
                    break;

                case Must.BeLessThan:
                    message = "Value must be less than {Argument}.";
                    break;

                case Must.BeLessThanOrEqualTo:
                    message = "Value must be less than or equal to {Argument}.";
                    break;

                case Must.BeEmpty:
                    message = "@Field must be empty.";
                    break;

                case Must.NotBeEmpty:
                    message = "@Field cannot be empty.";
                    break;

                default:
                    message = "@Invalid value.";
                    break;
                }
            }

            {
                var func            = new Func <IResourceContext, IProxy>(ValueProvider);
                var boundExpression = BoundExpression.Parse(message, new Dictionary <string, object>
                {
                    ["Value"]    = func,
                    ["Argument"] = argumentProvider
                });

                if (boundExpression.IsPlainString)
                {
                    var errorMessage = boundExpression.StringFormat;
                    errorProvider = context => new PlainErrorStringProvider(errorMessage);
                }
                else
                {
                    if (boundExpression.Resources.Any(
                            res => res is DeferredProxyResource resource && resource.ProxyProvider == func))
                    {
                        errorProvider =
                            context => new ValueErrorStringProvider(boundExpression.GetStringValue(context),
                                                                    ValueProvider(context));
                    }
 public ValueErrorStringProvider(StringProxy messageProxy, BindingProxy valueProxy)
 {
     this.messageProxy = messageProxy;
     this.valueProxy   = valueProxy;
 }
 public CompiledRegexReplacement(StringProxy pattern, StringProxy replacement, BindingProxy regexOptions)
 {
     this.pattern      = pattern;
     this.replacement  = replacement;
     this.regexOptions = regexOptions;
 }
Ejemplo n.º 10
0
        /// <summary>
        /// Можно ли сохранить пользователя
        /// </summary>
        public bool CanSave(object parameter = null)
        {
            BindingProxy bindProxy = parameter as BindingProxy;

            if (bindProxy.Data is PgUserM)
            {
                EvaluateError = null;
                PgUserM pgUser  = bindProxy.Data as PgUserM;
                bool    canSave = true;
                canSave &= !String.IsNullOrEmpty(pgUser.Login);
                canSave &= !String.IsNullOrEmpty(pgUser.NameFull);
                canSave &= !String.IsNullOrEmpty(pgUser.Otdel);
                canSave &= !String.IsNullOrEmpty(pgUser.WindowName);
                canSave &= (pgUser.Type != 0);
                if (!canSave)
                {
                    EvaluateError = "Введите обязательные поля";
                }

                bool ok = true;
                if (pgUser != null && pgUser.Login != null && pgUser.Login.Length > 0)
                {
                    if ((pgUser.Login[0] >= 'a' && pgUser.Login[0] <= 'z') || (pgUser.Login[0] == '_'))
                    {
                        for (int i = 1; i < pgUser.Login.Length; i++)
                        {
                            if (!((pgUser.Login[i] >= 'a' && pgUser.Login[i] <= 'z') ||
                                  (pgUser.Login[i] >= '0' && pgUser.Login[i] <= '9') ||
                                  pgUser.Login[i] == '_'))
                            {
                                ok = false;
                            }
                        }
                    }
                    else
                    {
                        ok = false;
                    }
                    if (!ok)
                    {
                        if (String.IsNullOrEmpty(EvaluateError))
                        {
                            EvaluateError = Rekod.Properties.Resources.LocUserLoginWarning;
                        }
                    }
                }
                else
                {
                    ok = false;
                }
                canSave &= ok;

                if (pgUser.IsNewUser && String.IsNullOrEmpty(pgUser.Pass))
                {
                    canSave &= false;
                    if (String.IsNullOrEmpty(EvaluateError))
                    {
                        EvaluateError = "Пароль не должен быть пустым";;
                    }
                }
                if (canSave)
                {
                    EvaluateError = null;
                }
                return(canSave);
            }
            else
            {
                EvaluateError = null;
                return(false);
            }
        }