Databind GradientStop Colors

I want to be able to databind a gradientstop's color to my template's background color, but I want to keep the alpha value of the gradient.This following does not work in XAML, but is there another way to do it?

<GradientStop Offset="0">
<GradientStop.Color>
<Color A="255" R="{Binding RelativeSource={RelativeSource TemplatedParent},Path=Background.Color.R}" G="{Binding RelativeSource={RelativeSource TemplatedParent},Path=Background.Color.G}" B="{Binding RelativeSource={RelativeSource TemplatedParent},Path=Background.Color.B}"//>
</GradientStop.Color>
</GradientStop>

[754 byte] By [JeremiahMorrill] at [2008-1-8]
# 1

Hi,

You could implement your binding with:

Code Snippet

<GradientStop Offset="0" Color="{Binding RelativeSource={RelativeSource TemplatedParent}}"/>

where myColorConverter would implement IValueConverter, something like this:

(Note that you can pass an argument, which could be the gradient value when calling a Converter)

Code Snippet

[ValueConversion(typeof(Color), typeof(Color))]
public class MyColorConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
Color originalColor = (Color) value;
Color myColor = Color.FromArgb(255, originalColor.R, originalColor.G, originalColor.B);
return myColor;
}

public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{ throw new Exception("Not implemented"); }
}

Luc
LucVoVan at 2007-10-2 > top of Msdn Tech,Visual Studio Orcas,Windows Presentation Foundation (WPF)...
# 2
Thanks! Is it possible to supply the Alpha channel as a parameter in the XAML?
JeremiahMorrill at 2007-10-2 > top of Msdn Tech,Visual Studio Orcas,Windows Presentation Foundation (WPF)...
# 3

Yes you can:

<GradientStop Offset="0" Color="{Binding RelativeSource={RelativeSource TemplatedParent}, Converter={StaticResource MyColorConverter}, ConverterParameter=128}"/>

LucVoVan at 2007-10-2 > top of Msdn Tech,Visual Studio Orcas,Windows Presentation Foundation (WPF)...

Visual Studio Orcas

Site Classified