Why doesn’t Color Binding work in a UWP application?

Well, it actually does;) If you try to set the color of control from a property of the model and it doesn’t work, then it is likely because you bind to a color object (e.g. Windows.UI.Color or System.Drawing.Color). Binding to these object types will fail, since all Color properties of controls (e.g. Foreground, Background, Fill, etc.) are actually Brush properties. Thus, you must bind to Brush objects like SolidColorBrush to achieve the effect.

Hier is a short example on how to instantiate a Brush (SolidColorBrush in that case)

public class NiceText
    public string Text { get; set; }
    public Brush GreenBrush { get; set; }

    public Answer()
        GreenBrush = new SolidColorBrush(Windows.UI.Colors.Green);

Then you can bind that property, for instance, to the Foreground property of a TextBlock.

<TextBlock Text="{Binding Path=Text}" Foreground="{Binding Path=GreenBrush}"/>

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.