For this to happen, you have two options.
Bind Brush display elements to Brush property in data
The data source has a property that provides the brush that you want to use for each element, and you bind the property of the display element that the brush takes, say, the Fill property. This works if the set of different values ββthat you would have for pairs of Start and Stop values ββis small. You would create an instance of each brush for each pair, and then the data element would display the correct one.
Bind Brush Display Elements Using Value Converter
If the Start and Stop values ββmean more than a variable, you will need a new instance of the Brush type for each item displayed. In this case, you bind the brush property of the display elements using a value converter, for example: -
<Rectangle Fill="{Binding Converter={StaticResource MyBrushBuilder} }" ... >
Pay attention to for a full description of the construction of the converter.
In this case, the implementation of your conversion method will look like this: -
public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { YourItemsType item = (YourItemsType)value; var start = new GradientStop(); start.Offset = 0; start.Color = item.GradientStart; var stop = new GradientStop(); stop.Offset = 1; stop.Color = item.GradientStop; var result = new RadialGradientBrush(); result.GradientOrigin = new Point(0.20, 0.5); result.Center = new Point(0.25, 0.5); result.RadiusX = 0.75; result.RadiusY = 0.5; result.GradientStops = new GradientStopCollection(); result.GradientStops.Add(start); result.GradientStops.Add(stop); return result; }
Caveat
Whenever data is bound, a whole bunch of brushes are created, one for each element. This can be costly and undesirable. Therefore, if this binding converter approach is considered necessary, I would recommend using a static brush dictionary. The key to this dictionary will be a hash of two colors. If necessary, you will create a new brush and reuse the previously created brush, if possible.