using System; using System.ComponentModel; using System.Globalization; using System.Windows; using System.Windows.Data; namespace AIStudio.Wpf.DiagramDesigner.Converters { #region Half public class HalfConverter : IValueConverter { public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { var doubleValue = (value as double?).GetValueOrDefault(); return doubleValue / 2; } public object ConvertBack(object value, Type targetTypes, object parameter, CultureInfo culture) { return DependencyProperty.UnsetValue; } } #endregion #region String IsNullOrEmpty public class IsNullOrEmptyConverter : IValueConverter { public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { return string.IsNullOrEmpty((string)value); } public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) { return DependencyProperty.UnsetValue; } } #endregion #region True -> False public class BoolInverseConverter : IValueConverter { public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { return !(bool)value; } public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) { return DependencyProperty.UnsetValue; } } #endregion #region True -> Visible Or Collpased public class BoolToVisibleConverter : IValueConverter { public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { return (bool)value ? Visibility.Visible : Visibility.Collapsed; } public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) { return DependencyProperty.UnsetValue; } } public class BoolToInvisibleConverter : IValueConverter { public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { return (bool)value ? Visibility.Collapsed : Visibility.Visible; } public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) { return DependencyProperty.UnsetValue; } } #endregion #region object try to convert to image (if is uri) public class IconConverter : IValueConverter { public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { if (value == null || !(value.GetType() == typeof(string))) return value; var iconString = value as string; if (!string.IsNullOrEmpty(iconString) && Uri.IsWellFormedUriString(iconString, UriKind.RelativeOrAbsolute)) { var image = new System.Windows.Controls.Image() { Source = new System.Windows.Media.Imaging.BitmapImage(new Uri(iconString, UriKind.RelativeOrAbsolute)), }; System.Windows.Media.RenderOptions.SetBitmapScalingMode(image, System.Windows.Media.BitmapScalingMode.HighQuality); return image; } return value; } public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) { return DependencyProperty.UnsetValue; } } #endregion #region Double -> GridLength public class GridLengthConverter : IValueConverter { public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { var length = value?.ToString(); try { return (GridLength)TypeDescriptor.GetConverter(typeof(GridLength)).ConvertFromString(length); } catch { return new GridLength(1, GridUnitType.Auto); } } public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) { return DependencyProperty.UnsetValue; } } #endregion #region Combiner public class MultiCombinerConverter : IMultiValueConverter { public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture) { return values.Clone(); } public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture) { return new object[] { DependencyProperty.UnsetValue, DependencyProperty.UnsetValue }; } } #endregion #region NotAlignmentCenter public class NotAlignmentCenterConverter : IValueConverter { public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { if (value is HorizontalAlignment) { var alignment = (HorizontalAlignment)value; return alignment != HorizontalAlignment.Center; } else if (value is VerticalAlignment) { var alignment = (VerticalAlignment)value; return alignment != VerticalAlignment.Center; } else return true; } public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) { return DependencyProperty.UnsetValue; } } #endregion }