0

I have bind an ellipse to a checkbox and an iValueConverter (this works ... Fill(see below)).

 <Ellipse Name="ellLeftRoleEnabled" 
                 Fill="{Binding IsChecked, ElementName=btnRollLeftEnabled, Converter={StaticResource myColorConverter}}" 
                 Height="80" Canvas.Left="355" Stroke="#FF0C703E" Canvas.Top="440" Width="80"/>

But now, how can I use this for a LinearGradientBrush/GradientStop?

<Ellipse Name="ellLeftRoleMoving" Height="100" Canvas.Left="345" Stroke="Black" Canvas.Top="535" Width="100">
            <Ellipse.Fill>
                <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                    <GradientStop Color="{?????} Offset="0"/>
                    <GradientStop Color="White" Offset="1"/>
                </LinearGradientBrush>
            </Ellipse.Fill>
        </Ellipse>

Please help. Thank You.

user1562809
  • 107
  • 9

2 Answers2

1

When using it for Color of a GradientStop Color you shouldn't return a Brush like the first converter but a Color. The rest is the same.

Giangregorio
  • 1,482
  • 2
  • 11
  • 12
1

Your converter should return a LinearGradientBrushinstead of a SolidColorBrushand keep you xaml as it is

public class myColorConverter:IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        return ((bool) value)
            ? new LinearGradientBrush()
            {
                EndPoint = new Point(0.5, 1),
                StartPoint = new Point(0.5, 0),
                GradientStops = new GradientStopCollection()
                {
                    new GradientStop(Colors.Red, 0),
                    new GradientStop(Colors.White, 1)
                }
            }
            : new LinearGradientBrush()
            {
                EndPoint = new Point(0.5, 1),
                StartPoint = new Point(0.5, 0),
                GradientStops = new GradientStopCollection()
                {
                    new GradientStop(Colors.Blue, 0),
                    new GradientStop(Colors.Red, 1)
                }
            };
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

Xaml

 <Ellipse Name="ellLeftRoleEnabled" 
             Fill="{Binding IsChecked, ElementName=btnRollLeftEnabled, Converter={StaticResource myColorConverter}}" 
             Height="80" Canvas.Left="355" Stroke="#FF0C703E" Canvas.Top="440" Width="80"/>
SamTh3D3v
  • 9,854
  • 3
  • 31
  • 47