It is possible to manage the logical resources from the code behind. You can call the FindResource method, passing the resource key to it, to get the instance of the resource. Here's how you can find the resource named myLinearBrush:
var resource = FindResource("myLinearBrush") as Brush;
You can also programmatically add or remove a resource to the collection. Call the methods Resources.Add and Resources.Remove to add or remove a specific resource, as shown in the following code snippet:
Resources.Add("myBrush", new SolidColorBrush(Colors.Red)); Resources.Remove("myBrush");
As the Resources property is basically a Dictionary object, make sure you check whether the specified key is already present before doing any operation, such as Add/Remove, on it.