static ObjectConverters() { Type = new RelayValueConverter((value, targetType, parameter, culture) => value?.GetType(), (value, targetType, parameter, culture) => { if (value is Type type) { return(Activator.CreateInstance(type)); } return(null); }); Exist = new RelayValueConverter((value, targetType, parameter, culture) => value != null); UnsetIfNull = new RelayValueConverter((value, targetType, parameter, culture) => value ?? DependencyProperty.UnsetValue); ParameterIfNull = new RelayValueConverter((value, targetType, parameter, culture) => value ?? parameter); AssignableFrom = new RelayValueConverter((value, targetType, parameter, culture) => { var valueType = value?.GetType(); var parameterType = parameter as Type; if (valueType == null || parameterType == null) { return(false); } return(parameterType.IsAssignableFrom(valueType)); }); Format = new RelayValueConverter(FormatValue); }
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); }); }
static TypeConverters() { DisplayName = new RelayValueConverter((value, targetType, parameter, culture) => { if (value is Type type) { return(type.GetDisplayName()); } return(null); }); EnumValues = new RelayValueConverter((value, targetType, parameter, culture) => { if (value is Type type && type.IsEnum) { return(type.GetEnumValues()); } return(null); }); }
static BoxConverters() { StringToInt32 = new RelayValueConverter( (value, targetType, parameter, culture) => Convert <String, Int32>(value, IsNullable(targetType), culture), (value, targetType, parameter, culture) => Convert <Int32, String>(value, IsNullable(targetType), culture)); Int32ToString = new RelayValueConverter( (value, targetType, parameter, culture) => Convert <Int32, String>(value, IsNullable(targetType), culture), (value, targetType, parameter, culture) => Convert <String, Int32>(value, IsNullable(targetType), culture)); StringToDecimal = new RelayValueConverter( (value, targetType, parameter, culture) => Convert <String, Decimal>(value, IsNullable(targetType), culture), (value, targetType, parameter, culture) => Convert <Decimal, String>(value, IsNullable(targetType), culture)); DecimalToString = new RelayValueConverter( (value, targetType, parameter, culture) => Convert <Decimal, String>(value, IsNullable(targetType), culture), (value, targetType, parameter, culture) => Convert <String, Decimal>(value, IsNullable(targetType), culture)); DoubleToGridLength = new RelayValueConverter( (value, targetType, parameter, culture) => value is double d ? new GridLength(d) : GridLength.Auto, (value, targetType, parameter, culture) => value is GridLength gridLength ? gridLength.Value : 0d); }
static ImageConverters() { Generic = new RelayValueConverter((value, targetType, parameter, culture) => { if (value is ResourceDescriptor resource) { value = resource.GetResource(); } if (value == null) { return(null); } targetType = parameter as Type ?? targetType; var converter = TypeDescriptor.GetConverter(value.GetType()); if (converter.CanConvertTo(targetType)) { return(converter.ConvertTo(value, targetType)); } if (typeof(ImageSource).IsAssignableFrom(targetType)) { if (value is Geometry geometry) { return(new DrawingImage { Drawing = new GeometryDrawing { Brush = new SolidColorBrush(Theme.Current.Colors.MainForeground), Geometry = geometry } }); } } return(value); }); }
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(); }); }
static ColorConverters() { const double defaultCoefficient = 0.2; Opacity = new RelayValueConverter((value, targetType, parameter, culture) => { if (!(parameter is double coefficient)) { coefficient = defaultCoefficient; } coefficient = Math.Max(Math.Min(coefficient, 1), 0); var sourceColor = value.ToColor(); if (sourceColor == null) { return(null); } var targetColor = ApplyOpacity(sourceColor.Value, coefficient); return(PrepareTargetColor(targetColor, targetType ?? value.GetType())); }); Generic = new RelayValueConverter((value, targetType, parameter, culture) => { var sourceColor = value.ToColor(); if (sourceColor == null) { return(null); } targetType = parameter as Type ?? targetType ?? value.GetType(); return(PrepareTargetColor(sourceColor.Value, targetType)); }); Shade = new RelayValueConverter((value, targetType, parameter, culture) => { if (!(parameter is double coefficient)) { coefficient = defaultCoefficient; } coefficient = Math.Max(Math.Min(coefficient, 1), 0); var sourceColor = value.ToColor(); if (sourceColor == null) { return(null); } var targetColor = ApplyShade(sourceColor.Value, coefficient); return(PrepareTargetColor(targetColor, targetType ?? value.GetType())); }); ContrastForeground = new RelayValueConverter((value, targetType, parameter, culture) => { var sourceColor = value.ToColor(); if (sourceColor == null) { return(null); } var targetColor = Colors.Black; var brightness = CalculateBrightness(sourceColor.Value); var luminance = CalculateLuminance(sourceColor.Value); if (brightness < 0.5 && luminance < 0.5) { targetColor = Colors.White; } if (brightness > 0.5 && luminance > 0.5) { targetColor = Colors.Black; } if (brightness > 0.5 && luminance < 0.5) { targetColor = Colors.White; } if (brightness < 0.5 && luminance > 0.5) { targetColor = Colors.Black; } return(PrepareTargetColor(targetColor, targetType ?? value.GetType())); }); Tint = new RelayValueConverter((value, targetType, parameter, culture) => { if (!(parameter is double coefficient)) { coefficient = defaultCoefficient; } coefficient = Math.Max(Math.Min(coefficient, 1), 0); var sourceColor = value.ToColor(); if (sourceColor == null) { return(null); } var targetColor = ApplyTint(sourceColor.Value, coefficient); return(PrepareTargetColor(targetColor, targetType ?? value.GetType())); }); Invert = new RelayValueConverter((value, targetType, parameter, culture) => { var sourceColor = value.ToColor(); if (sourceColor == null) { return(null); } var targetColor = ApplyInvert(sourceColor.Value); return(PrepareTargetColor(targetColor, targetType ?? value.GetType())); }); }
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)); }