silverlightxamlbindinggradientgradientstop

How to bind GradientStop Colours or GradientStops Property in Silverlight?


I want to be able to have a dynamic Gradient in Silverlight, such as below:

<RadialGradientBrush GradientOrigin="0.20,0.5" Center="0.25,0.50" 
                     RadiusX="0.75" RadiusY="0.5">
  <GradientStop Color="{Binding Path=GradientStart}" Offset="0" />
  <GradientStop Color="{Binding Path=GradientEnd}" Offset="1" />
</RadialGradientBrush>

I am binding to two properties which return the type "Color" however I always get this message:

AG_E_PARSER_BAD_PROPERTY_VALUE

If I try to bind to a GradientStop Collection this also has the same problem, what is the solution to this problem that:

  1. Allows the start and end of a Gradient to be changed at runtime
  2. Works in Silverlight 3.0 and is not a WPF solution

If there is a work around or anyway to duplicate this behaviour this would be acceptable, I have solutions that work with LinearGradients as I can just bind somethings "Fill" property to this - however in this situation that won't work, plus there may be other gradient types I may use and others may use in future which this solution / alternative will apply to.


Solution

  • The problem is that GradientStop does not derive from FrameworkElement therefore cannot be data bound. Unfortunately that means you have to set it from code.