Blue Theme Orange Theme Green Theme Red Theme
 
DevExpress Free UI Controls
Home | Forums | Videos | Advertise | Certifications | Downloads | Blogs | Interviews | Jobs | Beginners | Training
 | Consulting  
Submit an Article Submit a Blog 
 Jump to
Skip Navigation Links
TechnologyExpand Technology
WebsiteExpand Website
Team Foundation Server Hosting
Search :       Advanced Search »
Home » WPF » ResourceDictionary in WPF

ResourceDictionary in WPF

In this article you will learn how to use the ResourceDictionary in WPF.

Author Rank :
Page Views : 22242
Downloads : 298
Rating :
 Rate it
Level : Intermediate
   Print Read/Post comments Post a comment  Similar Articles  
   Email to a friend  Bookmark  Author's other articles  
Download Files:
ResourceDictionary in WPF.zip
 
 
Nevron Chart
Become a Sponsor
 Tag Cloud
 Latest Jobs
More ... 
 Latest Interview Questions
More ... 


The items in a ResourceDictionary are not immediately processed when application code is loaded by a XAML loader. Instead, the ResourceDictionary persists as an object, and the individual values are processed only when they are specifically requested.

The ResourceDictionary class is not derived from DictionaryBase. Instead, the ResourceDictionary class implements IDictionary but relies on a Hashtable internally.

In Extensible Application Markup Language (XAML), the ResourceDictionary class is typically an implicit collection element that is the object element value of several Resources properties, when given in property element syntax. For details on implicit collections in XAML, see XAML Syntax Terminology. An exception is when you want to specify a merged dictionary; for details, see Merged Resource Dictionaries.

Another possible XAML usage is to declare a resource dictionary as a discrete XAML file, and either load it at run time with Load or include it in a (full-trust) project as a resource or loose file. In this case, ResourceDictionary can be declared as an object element, serving as the root element of the XAML. You must map the appropriate XML namespace values (default for the WPF namespace and typically x: for the XAML namespace) onto the Resource Dictionary element if you plan to use it as the root element. Then you can add child elements that define the resources, each with an x:Key value.

Getting Started:

First of all make a new WPF Application and add a new ResourceDictionary file. I am putting my ResourceDictionary File in the Themes directory. Like figure1.

WpfResources1.gif

Figure1.

Here is my ResourceDictionary file XML code:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:col="clr-namespace:System.Collections;assembly=mscorlib"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
   <!--This is for gradient button style-->
    <ControlTemplate x:Key="buttonTemplate" TargetType="{x:Type Button}">
        <Grid>
            <Ellipse x:Name="outerCircle">
                <Ellipse.Fill>
                    <LinearGradientBrush StartPoint="0,0" EndPoint="0,1">
                        <GradientStop Offset="0" Color="Blue" />
                        <GradientStop Offset="1" Color="Red" />
                    </LinearGradientBrush>
                </Ellipse.Fill>
            </Ellipse>
            <Ellipse x:Name="innerCircle" RenderTransformOrigin=".5,.5">
                <Ellipse.RenderTransform>
                    <ScaleTransform ScaleX=".8" ScaleY=".8" />
                </Ellipse.RenderTransform>
                <Ellipse.Fill>
                    <LinearGradientBrush StartPoint="0,0" EndPoint="0,1">
                        <GradientStop Offset="0" Color="Red" />
                        <GradientStop Offset="1" Color="Blue" />
                    </LinearGradientBrush>
                </Ellipse.Fill>
            </Ellipse>
            <Viewbox>
                <ContentPresenter Margin="{TemplateBinding Padding}" />
            </Viewbox>
        </Grid>
        <ControlTemplate.Triggers>
            <Trigger Property="IsMouseOver" Value="True">
                <Setter TargetName="outerCircle" Property="Fill" Value="Orange" />
            </Trigger>
            <Trigger Property="IsPressed" Value="True">
                <Setter Property="RenderTransform" >
                    <Setter.Value>
                        <ScaleTransform ScaleX=".9" ScaleY=".9" />
                    </Setter.Value>
                </Setter>
                <Setter Property="RenderTransformOrigin" Value=".5,.5"/>
            </Trigger>
        </ControlTemplate.Triggers>
    </ControlTemplate>

     <!--This style used for normal button style-->
    <Style x:Key="buttonStyle" TargetType="{x:Type Button}">
        <Setter Property="Foreground" Value="White" />
        <Setter Property="Background" Value="Green" />
        <Setter Property="FontSize" Value="14" />
    </Style>

     <!--This style used for combo box collection style and list items-->
    <CollectionViewSource x:Key="myCol">       
       
<CollectionViewSource.Source>
            <col:ArrayList>               
               
<ListBoxItem>Raj Beniwal</ListBoxItem>
                <ListBoxItem>Vikash Nanda</ListBoxItem>
                <ListBoxItem>Amit Mishra</ListBoxItem>
                <ListBoxItem>Ketan Puri</ListBoxItem>
            </col:ArrayList>
        </CollectionViewSource.Source>
   </CollectionViewSource>

     <!--This style used for tab item styles-->
    <Style x:Key="tabItemStyle" TargetType="{x:Type TabItem}">
        <Setter Property="BorderBrush" Value="Blue" />
        <Setter Property="Background" Value="LightPink" />
        <Setter Property="FontSize" Value="14" />
        <Setter Property="Foreground" Value="IndianRed" />
    </Style>   
</ResourceDictionary>

You can initialise the path of Resource Dictionary in App.xaml file.

<Application x:Class="DataGridWithADONETEntityDataModel.App"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    StartupUri="ResourceDictionary.xaml">
    <Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="Themes/Dictionary1.xaml" />
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Application.Resources>
</Application>

Here is my .xaml code.

<Window x:Class="DataGridWithADONETEntityDataModel.ResourceDictionary"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:res="clr-namespace:DataGridWithADONETEntityDataModel"
        xmlns:col="clr-namespace:System.Collections;assembly=mscorlib"
    Title="ResourceDictionary" Height="500" Width="500">   
    <Grid>

 <ComboBox ItemsSource="{Binding Source={StaticResource myCol}}" Height="20" Margin="12,12,66,0" VerticalAlignment="Top"></ComboBox>

<
Button Margin="12,51,0,0" Height="40" Width="200" Name="button1"  Template="{StaticResource buttonTemplate}" HorizontalAlignment="Left" VerticalAlignment="Top">Click Me!</Button>

 <Button Margin="227,51,51,0" Name="button2"  Style="{StaticResource buttonStyle}" Height="23" VerticalAlignment="Top">Click Me!</Button>
       
<TabControl Height="295" Margin="12,145,30,22" Name="tabControl1" Width="436" Background="NavajoWhite">

 <TabItem Header="About Me" Name="tabItem1" Style="{StaticResource tabItemStyle}">
<Grid>
<TextBlock TextWrapping="Wrap">Rajkumar is working as a senior software engineer has over 6 years experience working on ASP.NET, VB.NET, C#, AJAX and other latest technologies. He holds Master's degree in Computer Science. currently enjoying working on WPF, WCF, Silverlight, MVC, XAML.</TextBlock>
                </Grid>
            </TabItem>

<TabItem Header="Contact Me" Name="tabItem2"  Style="{StaticResource tabItemStyle}">
                <Grid>
                    <TextBlock TextWrapping="Wrap">I can be reached on at raj2511984 at yahoo.com OR raj2511984@gmail.com</TextBlock>
                </Grid>
            </TabItem>

<
TabItem Header="Fav Pictures" Name="tabItem3"  Style="{StaticResource tabItemStyle}">
                <Grid>                   
                   
<Image Source="Neha-Dhupia-20091231-001.jpg" Margin="0,0,200,0"></Image>
                    <Image Source="Neha-Dhupia-20091231-003.jpg"  Margin="180,0,0,0"></Image>                                      
                </Grid>
            </TabItem>
           
<TabItem Header="My Hobbies" Name="tabItem4"  Style="{StaticResource tabItemStyle}">
                <Grid>
                    <TextBlock TextWrapping="Wrap">MY PASSION'S MUSIC TRANSCENDS BETWEEN HEIGHTS OF HEAVEN AND DEPTHS OF HELL! ONE MINUTE YOU'RE AT THE GREATEST PARTY ON EARTH AND YOUR WORLD IS FILLED WITH JOY, THE NEXT YOURE PLUNGED INTO A DARK DIRTY SCENE FROM A TIM BURTON FILM AND A NIGHTMARE THAT YOU DON'T WANT TO END! IT'S TIME TO START LIVING.</TextBlock>

                </Grid>
            </TabItem>
        </TabControl>
    </Grid>
</Window>

Now execute the application.

WpfResources2.gif

Comment Request!
Thank you for reading this post. Please post your feedback, question, or comments about this post Here.
Login to add your contents and source code to this article
 [Top] Rate this article
 
 About the author
 
Author
Looking for C# Consulting?
C# Consulting is founded in 2002 by the founders of C# Corner. Unlike a traditional consulting company, our consultants are well-known experts in .NET and many of them are MVPs, authors, and trainers. We specialize in Microsoft .NET development and utilize Agile Development and Extreme Programming practices to provide fast pace quick turnaround results. Our software development model is a mix of Agile Development, traditional SDLC, and Waterfall models.
Click here to learn more about C# Consulting.
 
Introducing MaxV - one click. infinite control. Hyper-V Hosting from MaximumASP.
Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Dynamic PDF
ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
Discover the Top 5 .NET Memory Management Fundamentals
To write the best .NET code, you need to know exactly how the .NET framework really manages memory. Ricky Leeks presents the Top 5 fundamental facts of .NET memory management. Learn more.
Nevron Chart for .NET 2010.1 Now Available
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.
ASP.NET 4 Hosting
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!
 
 Post a Feedback, Comment, or Question about this article
Subject:
Comment:
Discover the top 5 tips for understanding .NET Interop
Become a Sponsor
 Comments
Nevron Chart
 © 2012  contents copyright of their authors. Rest everything copyright Mindcracker. All rights reserved.