get phone accent brush programmatically c#

Isaiyavan Babu Karan picture Isaiyavan Babu Karan · Dec 26, 2012 · Viewed 14.2k times · Source

I have textbox in xaml

<TextBlock Style="{StaticResource PhoneTextExtraLargeStyle}" FontSize="{StaticResource PhoneFontSizeLarge}" FontFamily="{StaticResource PhoneFontFamilySemiLight}" Margin="12,10,12,0" />

How can I get value of phoneaccentbrush, programmatically (c#) from system resource of windows phone 7 / 7.5 / 8 so that i can set the foreground-color to match the accent selected in the WP's settings.

Answer

Spaso Lazarevic picture Spaso Lazarevic · Dec 26, 2012

First, you need to create currentAccentColorHex before Constructor of you C# class:

public partial class MainPage : PhoneApplicationPage
{
    Color currentAccentColorHex = (Color)Application.Current.Resources["PhoneAccentColor"];

    // Constructor
    public MainPage()
    {          
        //...

and then use it wherever you need to set color for the control: Example for Background property for control MyControl:

SolidColorBrush backColor = new SolidColorBrush(currentAccentColorHex);
MyControl.Background = backColor;

Hope this help