|
| 1 | +/* |
| 2 | + * COPYRIGHT: See COPYING in the top level directory |
| 3 | + * PROJECT: Common.Converter |
| 4 | + * FILE: ActiveToColorConverter.cs |
| 5 | + * PURPOSE: Convert a boolean value to a color for WPF bindings. |
| 6 | + * PROGRAMMER: Peter Geinitz (Wayfarer) |
| 7 | + */ |
| 8 | + |
| 9 | +using System; |
| 10 | +using System.Globalization; |
| 11 | +using System.Windows.Data; |
| 12 | +using System.Windows.Media; |
| 13 | + |
| 14 | +namespace Common.Converter |
| 15 | +{ |
| 16 | + /// <summary> |
| 17 | + /// Converter that converts a boolean value to a color for WPF bindings. It returns Gold for true (active) and Dark Gray for false (inactive). |
| 18 | + /// </summary> |
| 19 | + /// <seealso cref="System.Windows.Data.IValueConverter" /> |
| 20 | + public class ActiveToColorConverter : IValueConverter |
| 21 | + { |
| 22 | + /// <summary> |
| 23 | + /// Converts a value. |
| 24 | + /// </summary> |
| 25 | + /// <param name="value">The value produced by the binding source.</param> |
| 26 | + /// <param name="targetType">The type of the binding target property.</param> |
| 27 | + /// <param name="parameter">The converter parameter to use.</param> |
| 28 | + /// <param name="culture">The culture to use in the converter.</param> |
| 29 | + /// <returns> |
| 30 | + /// A converted value. If the method returns <see langword="null" />, the valid null value is used. |
| 31 | + /// </returns> |
| 32 | + public object Convert(object value, Type targetType, object parameter, CultureInfo culture) |
| 33 | + { |
| 34 | + if (value is bool isActive && isActive) |
| 35 | + { |
| 36 | + return new SolidColorBrush(Color.FromRgb(255, 215, 0)); // Gold for Active |
| 37 | + } |
| 38 | + return new SolidColorBrush(Color.FromRgb(60, 60, 60)); // Dark Gray for Inactive |
| 39 | + } |
| 40 | + |
| 41 | + /// <summary> |
| 42 | + /// Converts a value. |
| 43 | + /// </summary> |
| 44 | + /// <param name="value">The value that is produced by the binding target.</param> |
| 45 | + /// <param name="targetType">The type to convert to.</param> |
| 46 | + /// <param name="parameter">The converter parameter to use.</param> |
| 47 | + /// <param name="culture">The culture to use in the converter.</param> |
| 48 | + /// <returns> |
| 49 | + /// A converted value. If the method returns <see langword="null" />, the valid null value is used. |
| 50 | + /// </returns> |
| 51 | + public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) |
| 52 | + { |
| 53 | + if (value is SolidColorBrush brush) |
| 54 | + { |
| 55 | + var color = brush.Color; |
| 56 | + if (color == Color.FromRgb(255, 215, 0)) // Gold |
| 57 | + { |
| 58 | + return true; |
| 59 | + } |
| 60 | + if (color == Color.FromRgb(60, 60, 60)) // Dark Gray |
| 61 | + { |
| 62 | + return false; |
| 63 | + } |
| 64 | + } |
| 65 | + return false; |
| 66 | + } |
| 67 | + } |
| 68 | +} |
0 commit comments