user2877820
user2877820

Reputation: 307

Set resources dynamically in code using MVVM

So basically what I am trying to do is set a DynamicResource via code using the MVVM pattern.

Those are my resources:

<SolidColorBrush x:Key="UserGroupUserBrush" Color="Transparent"/>
<SolidColorBrush x:Key="UserGroupSetterBrush" Color="Yellow"/>
<SolidColorBrush x:Key="UserGrougShiftLeaderBrush" Color="{StaticResource ZFBlueColor}"/>
<SolidColorBrush x:Key="UserGroupTeamLeaderBrush" Color="Black"/>

Right here I want to set the resource:

<Ellipse DataContext="{vw:AdapterBinding UserManagementAdapter}" Fill="{DynamicResource UserGroupSetterBrush}" Height="20" Width="20" Margin="0,0,5,0"/>

So lets say I want to change the resource "UserGroupSetterBrush" to "UserGroupUserBrush" in my code. What is the correct way to do this using the MVVM pattern?

Upvotes: 1

Views: 221

Answers (1)

mm8
mm8

Reputation: 169370

What is the correct way to do this using the MVVM pattern?

In the code-behind of some view or some view-related class. Resources and brushes are part of the view only. The view model doesn't know nor care about these at all so there is basically no difference att all using or setting resources between a MVVM and a non-MVVM application.

The view model should not change the Fill property of an Ellipse. It may change a source property that the Fill property is bound to, but then you shouldn't set the property using a DynamicResource but bind it to a Brush source property of the view model and set this one in the view model.

Upvotes: 2

Related Questions