How to I bind the Background color of a button to a class property that can change?

Confession: I'm new to WPF.

I have a class and a property (shortened for convenience)

Class UserPreference
   Property ButtonColor as String

and I have a button whose Background property should be set to the property.

What's the best way to do this with XAML? The ButtonColor property might change and the button must reflect this change.


Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

1) Add binding to button Background property
<Button x:Name="button1" Background="{Binding ButtonColor, Mode=OneWay}" Content="Ok" />
2) Implement INotifyPropertyChanged interface in your UserPreference class.
When ButtonColor property changed you should raise PropertyChanged event.
If PropertyChanged <> Nothing Then
     PropertyChanged(Me, New PropertyChangedEventArgs("ButtonColor"))


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial

I think if you bind to the Background property you need to convert to a brush - or your binding source needs to be a brush (SolidColorBrush, LinearGradiant etc...).
However, if you have the color property - or only want to deal with colors, you can provide the brush in your Xaml and bind the color property of that;


        <Button x:Name="myBtn">
                <SolidColorBrush Color="{Binding Path=ButtonColor}" />

Open in new window

AIBMassAuthor Commented:
Very good reference provided.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Development

From novice to tech pro — start learning today.