GilAv
GilAv

Reputation: 13

WPF setting Brush from ResourceDictionary to a property in a ViewModel

I'm making a View with buttons and I want to change their color on click. I want the buttons to have a default color,and on first click will change their color to another.

In order to do so, I wanted to keep it clean so, I saved the brush in a resourceDictionary.

<ResourceDictionary 
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

     <SolidColorBrush x:Key="WeekCalendarDefaultCellColor" Color="#FFE5CC"/>
     <SolidColorBrush x:Key="WeekCalendarClickCellColor" Color="#FFFF00"/>

</ResourceDictionary>

for MVVM, I bind my button brush to a property (if I set a color by myself the binding works, but I want to use the same colors in the whole application so I think its better to take it from the dictionary)

public SolidColorBrush CurrentBrush =//????;

Now I want to insert the Brush from my dictionary to this property, how can I fetch the Brush from the Dictionary to the view model?

Thanks in advance to all the helpers !

Upvotes: 1

Views: 1597

Answers (1)

Jonatan Dragon
Jonatan Dragon

Reputation: 5017

This code would work if you use it in code behind:

button.Background = (Brush)FindResource("ButtonNormalBackgroundBrush");

BUT! You are saying you want to make it clean.

for MVVM, I bind my button brush to a property

You should not bind button brush to VM property. Brush is GUI (View in MVVM) part. VM should contain kind of state, like bool or enum, etc. Than you could read this state in button style and use triggers to change background.

Upvotes: 1

Related Questions