Binding to a SolidColorBrush and to Its Color

June 19, 2013

Translucent Background Color

While working on a WinRT project (Windows 8 Store App), I came across an interesting problem; how do I bind the Background property of a Border element to a SolidColorBrush but then set the opacity of that same brush to get the translucent look behind the text?

The answer came in a few steps. The first step was discovering how to create a background brush within the XAML data:

The code listing above shows the data template for the news item. I am still learning the fine details of XAML and data binding so some of what I show may be incomplete or non-standard. Anyhow, the StackPanel that contains the text is in the second column of the grid one line 10. The Background property is set by creating the SolidColorBrush right here in this XAML within the <StackPanel.Background> tag. By doing this, the color and opacity can be set separately.

This listing is not quite correct; what you see in the listing above is just a gray background color and not the blue tile color. The next step that did not work was to set the color to the same Binding Background value that is used for the square (see line 7 above where the square gets its background):

<SolidColorBrush Color="{Binding Background}" Opacity="0.30"/>

This obviously won’t work. It is not obvious here but “Background” is a property of the data item class and it is a SolidColorBrush, not a Color. This is probably obvious to others but I had to discover this myself. So I changed the code like this:

<SolidColorBrush Color="{Binding BackgroundColor}" Opacity="0.30"/>

And now one final step; create a BackgroundColor variable in the news item class so that the binding will work. To make a long story short, I create a BackgroundColor property and set it to the color of the brush that is used for the background:

This may look familiar for those who use the built-in pages when creating a Windows Store app in Visual Studio.

Line 11 is where the important step happens. The BackgroundColor property is set to the supplied brush color. Down at lines 42 and 52, the BackgroundColor property is handled in a similar fashion where setting the brush again sets the color but setting the color will also create a brush. This code doesn’t get used in my program because I don’t bind in both directions. Only the constructor code gets called (as far as I know).

This is a tiny bit ugly because the XAML relies on there being the very specific BackgroundColor property but this seems acceptable since it is handled by my data item base class and the XAML relies on other specific properties of that same base class.

And with the BackgroundColor property being used, this is how the tile, now in Green, looks:

Translucent Background Color Matching Background Brush

I opted to use a gray color in the end because shading to match the solid square color didn’t look as good to me. But given the right circumstance, this method could prove to be very useful and the green version is not really that bad.