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.

MainWindow.xaml



<Window x:Class="StaticResources.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Window.Resources>
        <SolidColorBrush x:Key="redbrush" Color="Red"></SolidColorBrush>
        <ImageBrush x:Key="Image" ImageSource="c:\AJAY DESAI JPAL PHOTO.jpg"></ImageBrush>
     </Window.Resources>
    <Grid>
        <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>
        
    </Grid>
</Window>

Output:









About the Author

I am Azeheruddin Khan having more than 6 year experience in c#, Asp.net and ms sql.My work comprise of medium and enterprise level projects using asp.net 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

0 comments:

adsense

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