Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 685
Sebastian Gray Avatar asked Aug 08 '09 06:08

Sebastian Gray


People also ask

How do I add resources to XAML dictionary?

Tip You can create a resource dictionary file in Microsoft Visual Studio by using the Add > New Item… > Resource Dictionary option from the Project menu. Here, you define a resource dictionary in a separate XAML file called Dictionary1.

What is brush in XAML?

Use Brush objects to paint the interiors and outlines of XAML shapes, text, and controls, making them visible in your application UI.

What is static resource in XAML?

A StaticResource will be resolved and assigned to the property during the loading of the XAML which occurs before the application is actually run. It will only be assigned once and any changes to resource dictionary ignored.


1 Answers

From within your UserControl:

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

Should do the trick.

like image 173
Matt Hamilton Avatar answered Oct 16 '22 14:10

Matt Hamilton