Sebastian Gray
Sebastian Gray

Reputation: 2704

How to use a defined brush resource in XAML, from C#

So far I have this

<UserControl.Resource>
 <LinearGradientBrush x:Key="KeyDownBrush" .....>

Now I would like to access this defined resource when a key is pressed and replace the current objects fill with the KeyDownBrush, in C#.

I've tried this.Resource.Contains("KeyDownPress") and have been able to get True returned so I presume I am almost there but I'm not sure how to access the object and Parse it correctly to a Brush instance.

Any guidance would be appreciated.

Upvotes: 8

Views: 7964

Answers (1)

Matt Hamilton
Matt Hamilton

Reputation: 204259

From within your UserControl:

var brush = this.Resources["KeyDownBrush"] as LinearGradientBrush;

Should do the trick.

Upvotes: 9

Related Questions