giacoder
giacoder

Reputation: 980

WP7 Binding in App.xaml Application.Resources

In App.xaml <Application.Resources>

I have:

<Color x:Key="ColorMain">#FF1F879C</Color>
<SolidColorBrush x:Key="ColorBrushMain" Color="{StaticResource ColorMain}"/>

then I have many templates which are using this brush and color. These templates are used all over the application.

I need to have an ability to change the color to change the skin of whole application. I need something like:

<SolidColorBrush x:Key="ColorBrushMain" Color="{Binding ColorMain}"/>

and in code something like:

 public string ColorMain {
    get {
       return ..... ; // "#FF803200";
    }
 }

But it doesn't work. Please help.

UPD: abhinav is right it must be a color

 public Color ColorMain {
    get {
       return ..... ; // return Color.FromArgb(0xFF, 0x80, 0x32, 0x00);
    }
 }

but this is not enough, it's not binding. I assume that it must be something as on normal page with DataContext to ViewModel, but what?

Upvotes: 0

Views: 811

Answers (2)

Henry C
Henry C

Reputation: 4801

If you're binding to a property that stores the Color and you're going to change at runtime and expect it to update, don't you need to be implementing INotifyPropertyChanged as well? For example:

public class MyViewModel: INotifyPropertyChanged

    private Color _mainColor
    public Color MainColor
    {
        get { return _mainColor; }
        set
        {
            if (value != _mainColor)
            {
                _mainColor= value;
                NotifyPropertyChanged("MainColor");
            }
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;
    private void NotifyPropertyChanged(String propertyName)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (null != handler)
        {
            handler(this, new PropertyChangedEventArgs(propertyName));
        }
    }

So: if you're expecting to change the color at runtime, use binding and implement INotifyPropertyChanged - if the colour isn't going to change at runtime, what you've already got should be fine.

Upvotes: 2

abhinav
abhinav

Reputation: 3217

You're binding a color property to a string object. Although I've never tried it, I'm quite sure that it will not work.

Maybe the documentation of the class will help. See this link.

Did you try using the color class instead?

    public Color ColorMain {
    get {
       return ..... ; // "#FF803200";
    }
 }

Upvotes: 1

Related Questions