use of boolean to color converter in XAML

后端 未结 3 633
甜味超标
甜味超标 2020-12-05 23:25

I am working on WPF application.I have bound my textblock to my button. I want to set foreground of my textblock to black color when its associated button\'s isEnabled is tr

3条回答
  •  离开以前
    2020-12-06 00:04

    To make this converter general you may use a ConverterParameter for specifying the colors which is to be inserted when value is true or false. Also the opacity may be of interest. I here provide the Converter I taking the parameter [ColorNameIfTrue;ColorNameIfFalse;OpacityNumber].

    Since the SolidColorBrush() method mentioned by @user1101511 is part of the System.Windows.Media library, it uses the Color type from that same library. This type does not have a Color.FromName() method, like the System.Drawing.Color class.

    Therfore I made a helper method called ColorFromName(string name). I specify "LimeGreen" as a fallback color if the interpertation of ConverterParameter fails. In my case I want the output to be "Transparent" when value is false.

    using System;
    using System.Globalization;
    using System.Windows.Data;
    using System.Windows.Media;
    
    namespace MyConverters
    {
        [ValueConversion(typeof(bool), typeof(SolidColorBrush))]
        class BoolToColorBrushConverter : IValueConverter
        {
            #region Implementation of IValueConverter
    
            /// 
            /// 
            /// 
            /// Bolean value controlling wether to apply color change
            /// 
            /// A CSV string on the format [ColorNameIfTrue;ColorNameIfFalse;OpacityNumber] may be provided for customization, default is [LimeGreen;Transperent;1.0].
            /// 
            /// A SolidColorBrush in the supplied or default colors depending on the state of value.
            public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            SolidColorBrush color;
            // Setting default values
            var colorIfTrue = Colors.LimeGreen;
            var colorIfFalse = Colors.Transparent;
            double opacity = 1;
            // Parsing converter parameter
            if (parameter != null)
            {
                // Parameter format: [ColorNameIfTrue;ColorNameIfFalse;OpacityNumber]
                var parameterstring = parameter.ToString();
                if (!string.IsNullOrEmpty(parameterstring))
                {
                    var parameters = parameterstring.Split(';');
                    var count = parameters.Length;
                    if (count > 0 && !string.IsNullOrEmpty(parameters[0]))
                    {
                        colorIfTrue = ColorFromName(parameters[0]);
                    }
                    if (count > 1 && !string.IsNullOrEmpty(parameters[1]))
                    {
                        colorIfFalse = ColorFromName(parameters[1]);
                    }
                    if (count > 2 && !string.IsNullOrEmpty(parameters[2]))
                    {
                        double dblTemp;
                        if (double.TryParse(parameters[2], NumberStyles.AllowDecimalPoint, CultureInfo.InvariantCulture.NumberFormat, out dblTemp))
                            opacity = dblTemp;
                    }
                }
            }
            // Creating Color Brush
            if ((bool) value)
            {
                color = new SolidColorBrush(colorIfTrue);
                color.Opacity = opacity;
            }
            else
            {
                color = new SolidColorBrush(colorIfFalse);
                color.Opacity = opacity;
            }
            return color;
        }
    
    
        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    
        #endregion
    
        public static Color ColorFromName(string colorName)
        {
            System.Drawing.Color systemColor = System.Drawing.Color.FromName(colorName);
            return Color.FromArgb(systemColor.A, systemColor.R, systemColor.G, systemColor.B);
        }
    }
    

    From xaml the above converter may be used like this:

    Background="{Binding MyBooleanValue, Converter={StaticResource BoolToColorBrushConverter}, ConverterParameter=LimeGreen;Transperent;0.2, Mode=OneWay}"
    

提交回复
热议问题