tribute2ro
tribute2ro

Reputation: 390

Set Control Styles to ThemeResource Values Programmatically In Windows Phone 8.1

I'm adding some controls dynamically in my Windows Phone 8.1 application. In the XAML, you can set various styles to the current theme's style just as I set the foreground attribute of this TextBlock control in the following example.

<TextBlock Text="Hello World" Foreground="{ThemeResource PhoneAccentBrush}" />

I want to be able to do the same thing in the code behind, but have not yet been able to determine how to do this. I create the TextBlock programmatically as follows.

TextBlock textBlock = new TextBlock() {
    Text = "Hello World",
    Foreground = // Need to get phone accent brush from theme
};

I've seen examples where different theme values are stored as follows, but this Dictionary doesn't seem to contain any keys when I checked for theme resources.

SolidColorBrush phoneAccent = new SolidColorBrush((Color)Application.Current.Resources["PhoneAccentColor"]);

Any help would be appreciated. Thank you!

Upvotes: 3

Views: 3303

Answers (2)

Jahus
Jahus

Reputation: 73

In Visual Basic .net:

.Foreground = CType(Resources.Item("PhoneAccentBrush"), Brush)

Upvotes: 0

Rob Caplan - MSFT
Rob Caplan - MSFT

Reputation: 21899

Load it from PhoneAccentBrush not PhoneAccentColor:

Brush accentBrush = Resources["PhoneAccentBrush"] as Brush;
TextBlock textBlock = new TextBlock()
{
    Text = "Hello World",
    Foreground = accentBrush 
};

Upvotes: 2

Related Questions