Every UI element derived from the FrameworkElement has a property called Resources, which is of type ResourceDictionary. Thus, every element can have resources associated with it. In XAML, we need to define the x:Key attribute to the resource to access it, either from the XAML or from the code-behind file.
In our example, we defined myLinearBrush as an element of the ResourceCollection of the Window. Thus, it will be accessible by any control within the same window. If you move the definition inside the StackPanel, it will be accessible within that panel:
<Window.Resources> <LinearGradientBrush x:Key="myLinearBrush"> <GradientStop Offset="0" Color="LightYellow"/> <GradientStop Offset="0.2" Color="Yellow"/> <GradientStop Offset=".5" Color="YellowGreen"/> <GradientStop...