Resources WPF in VB.NET
This article defines the static and dynamic resources in WPF and XAML.
This article defines the static and dynamic resources in WPF and XAML.
Resources
Window control uses the Resources property (<Window.Resources>) to define resource. Windows Presentation Foundation (WPF) resources provide a simple way to reuse commonly defined objects and values. Resources in WPF allow you to set the properties of multiple controls at a time. For example you can set the background property on several elements in a WPF application using a single resource.
creating static resource
The below xaml code defines the static resource.
Window control uses the Resources property (<Window.Resources>) to define resource.
keyName: key name of the resource, which is unique string to identify the resource.
Xaml code
<Window.Resources>
<GeometryDrawing x:Key="color" Brush="Red">
<GeometryDrawing.Geometry>
<EllipseGeometry RadiusX="200" RadiusY="10" />
</GeometryDrawing.Geometry>
</GeometryDrawing>
</Window.Resources>
The Key name is color in the above resource. using key name with StaticResource to apply the resource on the rectangle control.
<StackPanel Orientation="Vertical">
<Rectangle Width="250" Height="98">
<Rectangle.Fill>
<DrawingBrush Drawing="{StaticResource color}" />
</Rectangle.Fill>
</Rectangle>
</StackPanel>
Now run the application.
Figure1.gif
Creating dynamic resource
The below xaml code defines the dynamic resource.
XAML code
<Window.Resources>
<SolidColorBrush x:Key="roh" Color="green" />
</Window.Resources>
<Button Content="Button" Height="73" Name="Button1" Width="152" />
</Window>
Now click on the Button control and add the following code.
Class MainWindow
Private Sub Button1_Click_1(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles Button1.Click
Button1.Background = CType(Me.FindResource("roh"), SolidColorBrush)
End Sub
End Class
Now run the application.
Figure2.gif
Now click on the Button. the Window form looks like this.
Figure3.gif