Solibulo

Uninteresting things

Silverlight 3 - Tip of the month #1 – Binding Color property

by softlion 4. October 2009 08:04

In XAML you can not bind a property of type Color directly. I’m not sure why. Maybe because it is a structure (ie: value type).

You can bind a Brush instead :

 

[DataMember]
internal Color color;
public Brush DisplayColorBrush { get { return new SolidColorBrush(color); } }

 

Binding to a static color resource is working though:

<TextBlock.Foreground>
    <SolidColorBrush Color="{StaticResource UserOffersInfoColor}"/>
</TextBlock.Foreground>

 

<Application.Resources>
    <Color x:Key="UserOffersInfoColor">#FFE97F26</Color>
</Application.Resources>

 

Binding a Text property in a Run tag of a TextBlock tag doesn’t work either. You must bind to the Text property of the TextBlock tag instead.

<TextBlock Margin="6,3" FontWeight="Bold" FontSize="12">
    <Run Text="Chips: "/>
    <Run Text="{Binding ChipsRemaining}"/>
</TextBlock>