SIGN UP MEMBER LOGIN:    
ARTICLE

Using StaticResources in WPF

Posted by Jitendra Sampathirao Articles | WPF with C# October 26, 2010
Here I describe Resource markup extensions.
Reader Level:
 

Resources provide a simple way to reuse commonly defined objects and values. If we are defining the resource then we can use that resource a number of times. (Just like applying themes and css to web pages). 

 

Resources are two types:
 

      1. Static Resource

      2. Dynamic Resource.

 

StaticResource: StaticResources are resolved at compile time. Use StaticResources when it's clear that you don't need your resource re-evaluated when fetching it static resources perform better than dynamic resources.

 

DynamicResource: DynamicResources are resolved at runtime. Use DynamicResources when the value of the resource could change during the lifetime of the Application.

 

I will show you an example of a Static Resource.

 

In my WPF application page I have one textbox and two ellipses. In Window.Resources I defined the SolidColorBrush and the Style properties.

 

<Window.Resources>

<SolidColorBrush x:Key="brush" Color="Green"></SolidColorBrush>

<SolidColorBrush x:Key="forbuttoncolor" Color="CadetBlue"></SolidColorBrush>

<Style TargetType="Border" x:Key="background">

<Setter Property="Background" Value="Orange"></Setter>

</Style>

<Style TargetType="TextBox" x:Key="TitleText">

<Setter Property="Background" Value="White"/>

<Setter Property="DockPanel.Dock" Value="Top"/>

<Setter Property="FontSize" Value="14"/>

<Setter Property="Foreground" Value="#4E87D4"/>

<Setter Property="FontFamily" Value="Tahoma"/>

<Setter Property="Margin" Value="50,50,50,0"/>

<Setter Property="Width" Value="300"></Setter>

</Style>

</Window.Resources>

 

In above code you can observe x: Key attribute. x: Key uniquely identifies elements that are created and referenced in a XAML-defined dictionary.

 

Here I'm using above resources. Have a look at below code.  

Syntax is :{ StaticResource x:keyName}

 

<Grid>

<StackPanel>

<Border Style="{StaticResource background}">

<DockPanel Height="300" Width="500">

<TextBox Style="{StaticResource TitleText}" Height="28" Width="150"> </TextBox>

<Ellipse DockPanel.Dock="Left" HorizontalAlignment="Left" Height="73" Fill="{StaticResource brush}" Width="169"></Ellipse>

<Ellipse DockPanel.Dock="Right" HorizontalAlignment="Right" Width="149" Height="73" Fill="{StaticResource forbuttoncolor}"></Ellipse>

</DockPanel>

</Border>

</StackPanel>      

</Grid>

 

Result window looks like this.

 

Static Resource.bmp
 

In my very next article, I will explain you about Dynamic Resource.
 

Hope you like this article, let me know if you have any queries.

 

Login to add your contents and source code to this article
share this article :
post comment
 

I am a software student and found your article very useful.

Posted by Kieran Callaghan Oct 31, 2011

Really good article to understand Static Resources. Keep it up buddy :)

Posted by Sachin Kalia Feb 02, 2011

Thank you dude...........

Posted by Jitendra Sampathirao Dec 02, 2010

simple,claire et très intéressant

Posted by nesrine meliene Dec 02, 2010

Thanks for sharing man. Keep going.. :)

Posted by Sivaraman Dhamodaran Nov 24, 2010
Become a Sponsor
PREMIUM SPONSORS
  • The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
    Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites - Click Here!
Become a Sponsor