Thursday, November 10, 2016

How to use static resources within a XAML page using c#

7:49 AM - By azeheruddin khan 0

Explanation: - Resources provide a way to reuse commonly defined values for attributes or properties like color, image etc for different types of controls or elements. Examples for resources are brushes, styles and control template. These resources are defined in the Resources
section i.e. in <Window.Resources> </Window.Resources>.

In the above given program, SolidColorBrush is a resource which is used to provide a value for the attribute: color that can be reused by different types of elements or controls like label and textbox within framework-level elements like window or page. Now this resource called SolidColorBrush is uniquely identified by a key called redbrush.

The StaticResource markup extension processes a key by looking up the value for that key in all the available resource dictionaries and within framework-level elements like Window or Page. For ex: - color=”red”, this value for color is obtained by processing the key called redbrush.
Static references work best for the following circumstances:

· You do not intend to change the value of the resource after it is referenced for the first time.
· You are creating a resource dictionary that you will compile into a dynamic-link

library (DLL) which can be re-used.


<Window x:Class="StaticResources.MainWindow"
        Title="MainWindow" Height="350" Width="525">
        <SolidColorBrush x:Key="redbrush" Color="Red"></SolidColorBrush>
        <ImageBrush x:Key="Image" ImageSource="c:\AJAY DESAI JPAL PHOTO.jpg"></ImageBrush>
        <TextBox Height="300" FontStyle="Normal" FontFamily="Times New Roman" FontSize="26" Background="{StaticResource ResourceKey=Image}" Margin="125,-1,128,12" Foreground="{StaticResource ResourceKey=redbrush}"></TextBox>


About the Author

I am Azeheruddin Khan having more than 6 year experience in c#, and ms sql.My work comprise of medium and enterprise level projects using and other Microsoft .net technologies. Please feel free to contact me for any queries via posting comments on my blog,i will try to reply as early as possible. Follow me @fresher2programmer
View all posts by admin →

Get Updates

Subscribe to our e-mail newsletter to receive updates.

Share This Post



© 2014 Fresher2Programmer. WP Theme-junkie converted by Bloggertheme9
Powered by Blogger.
back to top