How do I convert a Color to a Brush in XAML?

前端 未结 7 1947
逝去的感伤
逝去的感伤 2020-11-27 05:52

I want to convert a System.Windows.Media.Color value to a System.Windows.Media.Brush. The color value is databound to a Rectangle object\'s Fill property. The Fill property

7条回答
  •  天涯浪人
    2020-11-27 06:01

    Converter:

    [ValueConversion(typeof(SolidColorBrush), typeof(Color))]
    public class SolidBrushToColorConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (!(value is SolidColorBrush)) return null;
            var result = (SolidColorBrush)value;
            return result.Color;
        }
    
        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
    

    XAML:

    //...
    
    //...
    
        
        
    
    //...
    

提交回复
热议问题