Exemple #1
0
        static StringConverters()
        {
            EmptyWatermark = new RelayValueConverter((value, targetType, parameter, culture) =>
            {
                var watermark   = parameter?.ToString() ?? "<Empty>";
                var stringValue = value?.ToString();

                return(string.IsNullOrEmpty(stringValue) ? watermark : stringValue);
            });

            UnsetIfEmpty = new RelayValueConverter((value, targetType, parameter, culture) =>
            {
                var stringValue = value?.ToString();
                return(string.IsNullOrEmpty(stringValue) ? DependencyProperty.UnsetValue : stringValue);
            });

            IsEmpty = new RelayValueConverter((value, targetType, parameter, culture) =>
            {
                var stringValue = value?.ToString();
                return(string.IsNullOrEmpty(stringValue));
            });

            DisplayName = new RelayValueConverter((value, targetType, parameter, culture) =>
            {
                if (value == null)
                {
                    return(string.Empty);
                }
                if (value is Type valueAsType)
                {
                    return(valueAsType.GetDisplayName());
                }

                var valueType = value.GetType();
                if (valueType.IsEnum)
                {
                    var fieldInfo = valueType.GetField(value.ToString());
                    if (fieldInfo != null)
                    {
                        return(fieldInfo.GetDisplayName());
                    }
                }

                return(value.ToString());
            });

            FirstNotEmpty = new RelayMultiValueConverter((objects, type, parameter, culture) =>
            {
                var result = objects.Enumerate().FirstOrDefault(o => !string.IsNullOrEmpty(o.GetEffectiveString()));
                return(result);
            });
        }
        static CollectionConverters()
        {
            Sort         = new RelayValueConverter((value, targetType, parameter, culture) => CreateView(value, parameter.Enumerate <SortDescription>()));
            Group        = new RelayValueConverter((value, targetType, parameter, culture) => CreateView(value, parameter.Enumerate <GroupDescription>()));
            GroupAndSort = new RelayValueConverter((value, targetType, parameter, culture) => CreateView(value, parameter.Enumerate()));

            Any     = new RelayValueConverter((value, targetType, parameter, culture) => value.Enumerate().Any());
            Count   = new RelayValueConverter((value, targetType, parameter, culture) => value.Enumerate().Count());
            Contain = new RelayMultiValueConverter((values, targetType, parameter, culture) =>
            {
                if (values.Length < 2)
                {
                    return(false);
                }

                var components = values[0].Enumerate().ToList();
                var element    = values[1];
                var treatNullElementNameAsPresentInCollection = parameter is bool boolParameter && boolParameter;
                if (treatNullElementNameAsPresentInCollection && element == null)
                {
                    return(true);
                }

                return(components.Contains(element));
            });
            DoesNotContain = new RelayMultiValueConverter((values, targetType, parameter, culture) =>
                                                          Contain.Convert(values, targetType, parameter, culture).AreEqual(false));
            FirstOrDefault = new RelayMultiValueConverter((values, targetType, parameter, culture) =>
            {
                if (values.Length < 2)
                {
                    return(false);
                }

                var components = values[0].Enumerate().ToList();
                var element    = values[1];

                var result = components.FirstOrDefault(e => e.AreEqual(element)) ??
                             components.Enumerate <IIDAware>().FirstOrDefault(c => c.Id.AreEqual(element));

                if (result == null && element is IIDAware idAwareElement)
                {
                    result = components.Enumerate <IIDAware>().FirstOrDefault(c => c.Id.AreEqual(idAwareElement.Id));
                }

                return(result);
            });
        }
Exemple #3
0
        static NumericConverters()
        {
            Invert = new RelayValueConverter(InvertFunc, InvertFunc);
            Abs    = new RelayValueConverter(AbsFunc, AbsFunc);

            Add             = new RelayValueConverter(AddFunc, SubtractFunc);
            Subtract        = new RelayValueConverter(SubtractFunc, AddFunc);
            Multiply        = new RelayValueConverter(MultiplyFunc, DivideFunc);
            Divide          = new RelayValueConverter(DivideFunc, MultiplyFunc);
            StringToNumeric = new RelayValueConverter(StringToNumericFunc, NumericToStringFunc);
            NumericToString = new RelayValueConverter(NumericToStringFunc, StringToNumericFunc);

            MultiAdd = new RelayMultiValueConverter((objects, type, parameter, culture) =>
            {
                var queue = new Queue <object>(objects.Enumerate().Select(obj =>
                {
                    if (obj == null)
                    {
                        return(0);
                    }
                    return(obj.GetType().IsNumeric() ? obj : 0);
                }));

                if (!queue.Any())
                {
                    return(null);
                }
                if (!queue.TryDequeue(out var result))
                {
                    return(null);
                }

                while (queue.TryDequeue(out var sub))
                {
                    result = (dynamic)result + (dynamic)sub;
                }

                return(result);
            });

            MultiSubtract = new RelayMultiValueConverter((objects, type, parameter, culture) =>
            {
                var queue = new Queue <object>(objects.Enumerate().Select(obj =>
                {
                    if (obj == null)
                    {
                        return(0);
                    }
                    return(obj.GetType().IsNumeric() ? obj : 0);
                }));

                if (!queue.Any())
                {
                    return(null);
                }
                if (!queue.TryDequeue(out var result))
                {
                    return(null);
                }

                while (queue.TryDequeue(out var sub))
                {
                    result = (dynamic)result - (dynamic)sub;
                }

                if (parameter is bool isPositive && isPositive)
                {
                    return((dynamic)result > 0 ? result : type.GetSystemDefaultValue());
                }
                return(result);
            });

            Min = new RelayValueConverter((value, type, parameter, culture) =>
            {
                var left  = (dynamic)(value ?? 0);
                var right = (dynamic)(parameter ?? 0);
                return(left < right ? left : right);
            });

            Max = new RelayValueConverter((value, type, parameter, culture) =>
            {
                var left  = (dynamic)(value ?? 0);
                var right = (dynamic)(parameter ?? 0);
                return(left > right ? left : right);
            });

            MultiMin = new RelayMultiValueConverter((objects, type, parameter, culture) =>
            {
                var itemsToCompare = objects.Where(item => item != null && item.GetType().IsNumeric())
                                     .ToList();
                object result = null;
                foreach (var item in itemsToCompare)
                {
                    if (result == null)
                    {
                        result = item;
                    }
                    if ((dynamic)item < (dynamic)result)
                    {
                        result = item;
                    }
                }

                return(result.ConvertNumericValueTo(type));
            });

            MultiMax = new RelayMultiValueConverter((objects, type, parameter, culture) =>
            {
                var itemsToCompare = objects.Where(item => item != null && item.GetType().IsNumeric())
                                     .ToList();
                object result = null;
                foreach (var item in itemsToCompare)
                {
                    if (result == null)
                    {
                        result = item;
                    }
                    if ((dynamic)item > (dynamic)result)
                    {
                        result = item;
                    }
                }

                return(result.ConvertNumericValueTo(type));
            });

            IsLessThan          = new RelayValueConverter((value, type, parameter, culture) => (dynamic)(value ?? 0) < (dynamic)(parameter ?? 0));
            IsLessThanOrEqual   = new RelayValueConverter((value, type, parameter, culture) => (dynamic)(value ?? 0) <= (dynamic)(parameter ?? 0));
            IsBiggerThan        = new RelayValueConverter((value, type, parameter, culture) => (dynamic)(value ?? 0) > (dynamic)(parameter ?? 0));
            IsBiggerThanOrEqual = new RelayValueConverter((value, type, parameter, culture) => (dynamic)(value ?? 0) >= (dynamic)(parameter ?? 0));
            IsEqual             = new RelayValueConverter((value, type, parameter, culture) => (dynamic)(value ?? 0) == (dynamic)(parameter ?? 0));

            ToGridLength = new RelayValueConverter((value, targetType, parameter, culture) =>
            {
                if (value == null)
                {
                    return(GridLength.Auto);
                }

                var converter = TypeDescriptor.GetConverter(typeof(GridLength));
                if (converter.CanConvertFrom(value.GetType()))
                {
                    return(converter.ConvertFrom(value));
                }

                throw new NotSupportedException();
            },
                                                   (value, targetType, parameter, culture) =>
            {
                if (value == null)
                {
                    return(GridLength.Auto);
                }

                var converter = TypeDescriptor.GetConverter(typeof(GridLength));
                if (converter.CanConvertFrom(value.GetType()))
                {
                    return(converter.ConvertFrom(value));
                }

                throw new NotSupportedException();
            });
        }
Exemple #4
0
        static BooleanConverters()
        {
            ToYesNo = new RelayValueConverter((value, targetType, parameter, culture) =>
            {
                if (value is bool boolean)
                {
                    return(boolean ? Yes : No);
                }
                if (value == null)
                {
                    return(Maybe);
                }

                throw new NotSupportedException();
            },
                                              (value, targetType, parameter, culture) =>
            {
                if (value is string stringValue)
                {
                    if (string.Equals(Yes, stringValue))
                    {
                        return(true);
                    }
                    if (string.Equals(No, stringValue))
                    {
                        return(false);
                    }
                    if (string.Equals(Maybe, stringValue))
                    {
                        return(null);
                    }
                }

                if (value == null)
                {
                    return(null);
                }

                throw new NotSupportedException();
            });

            ToVisibilityInverted = new RelayValueConverter((value, targetType, parameter, culture) =>
            {
                if (value is bool boolean)
                {
                    return(boolean ? Visibility.Collapsed : Visibility.Visible);
                }

                if (value == null)
                {
                    return(Visibility.Visible);
                }

                throw new NotSupportedException();
            },
                                                           (value, targetType, parameter, culture) =>
            {
                if (value is Visibility castedValue)
                {
                    switch (castedValue)
                    {
                    case Visibility.Visible:
                        return(false);

                    case Visibility.Hidden:
                    case Visibility.Collapsed:
                        return(true);
                    }
                }

                throw new NotSupportedException();
            });

            ToVisibility = new RelayValueConverter((value, targetType, parameter, culture) =>
            {
                if (value is bool boolean)
                {
                    return(boolean ? Visibility.Visible : Visibility.Collapsed);
                }

                if (value == null)
                {
                    return(Visibility.Collapsed);
                }

                throw new NotSupportedException();
            },
                                                   (value, targetType, parameter, culture) =>
            {
                if (value is Visibility castedValue)
                {
                    switch (castedValue)
                    {
                    case Visibility.Visible:
                        return(true);

                    case Visibility.Hidden:
                    case Visibility.Collapsed:
                        return(false);
                    }
                }

                throw new NotSupportedException();
            });

            Inverted = new RelayValueConverter((value, targetType, parameter, culture) =>
            {
                if (value is bool boolean)
                {
                    return(!boolean);
                }

                throw new NotSupportedException();
            },
                                               (value, targetType, parameter, culture) =>
            {
                if (value is bool boolean)
                {
                    return(!boolean);
                }

                throw new NotSupportedException();
            });

            MultiAnd = new RelayMultiValueConverter((values, type, parameter, culture) => values.Enumerate <bool>().All(value => value));
            MultiOr  = new RelayMultiValueConverter((values, type, parameter, culture) => values.Enumerate <bool>().Any(value => value));
        }