Using logical resources
WPF introduces the concept of logical resources, objects that can be shared (and reused) across some part of a visual tree or an entire application. Logical resources can be anything, from WPF objects such as brushes, geometries, or styles, to other objects defined by the .NET Framework or the developer, such as string
, List<>
, or some custom typed object. This gives a whole new meaning to the term resources. These objects are typically placed inside a ResourceDictionary
and located at runtime using a hierarchical search, as demonstrated in this recipe.
Getting ready
Make sure Visual Studio is up and running.
How to do it...
We'll create a simple application that demonstrates creating and using logical resources:
Create a new WPF Application named
CH02.SimpleResources
.Open
MainWindow.xaml
and replace theGrid
element with aStackPanel
.Add a
Rectangle
element to theStackPanel
, as shown in the following code snippet:<Rectangle Height="100" Stroke="Black">...