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.
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.
Use Brush objects to paint the interiors and outlines of XAML shapes, text, and controls, making them visible in your application UI.
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.
From within your UserControl:
var brush = this.Resources["KeyDownBrush"] as LinearGradientBrush;
Should do the trick.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With