Binding GradientStop works but reports error

后端 未结 1 1132
耶瑟儿~
耶瑟儿~ 2021-01-12 08:28

The following code binds a GradientStop to the Background.Color property of TemplatedParent. Everything works but I am getting a bindi

相关标签:
1条回答
  • 2021-01-12 09:19

    I also had the same error in the Visual Studio console output.

    A possible explanation and workaround for this is reported here

    Basically if you use a Converter that returns a LinearGradientBrush then you don't get the error

    The code is something like this

    [ValueConversion(typeof(System.Windows.Media.Color), typeof(LinearGradientBrush))]
    class GradientConverter : IValueConverter
    {
    
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            var brush = new LinearGradientBrush();
            var color = (Color)value;
            brush.StartPoint = new Point(0.5, 0);
            brush.EndPoint = new Point(0.5, 1);
    
            brush.GradientStops.Add(new GradientStop(Colors.White, 0));
            brush.GradientStops.Add(new GradientStop((Color)value, 1));
    
            return brush;
        }
    
        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
    

    And in the XAML

    <Border BorderThickness="1" BorderBrush="{TemplateBinding Background}" Background="{Binding Path=Background.Color, RelativeSource={RelativeSource TemplatedParent}, Converter={StaticResource gradConv}}">
    
    0 讨论(0)
提交回复
热议问题