Blue Theme Orange Theme Green Theme Red Theme
 
Ads by Lake Quincy Media
Home | Forums | Videos | Photos | Downloads | Blogs | Interviews | Jobs | Beginners | Training
 | Consulting  
Submit an Article Submit a Blog 
 Login Close
User Id:
Password:
 
Forgot Password
Forgot Username
Why Register
 Jump to
Skip Navigation Links
TechnologyExpand Technology
WebsiteExpand Website
 Resources  
Close
 Our Network  
Close
Search :       Advanced Search »
Home » WPF » ResourceDictionary in WPF

ResourceDictionary in WPF

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

Author Rank:
Total page views :  2393
Total downloads :  48
   Print Read/Post comments Post a comment  Similar Articles  
   Email to a friend  Bookmark  Author's other articles  
Download Files:
ResourceDictionary in WPF.zip
 
Become a Sponsor


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 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 application.

WpfResources2.gif 


Login to add your contents and source code to this article
 About the author
 
Raj Kumar
Rajkumar is working as a senior software engineer has over 5 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.I can be reached on at raj2511984 at yahoo.com
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.
Go.NET
Build custom interactive diagrams, network, workflow editors, flowcharts, or software design tools. Includes many predefined kinds of nodes, links, and basic shapes. Supports layers, scrolling, zooming, selection, drag-and-drop, clipboard, in-place editing, tooltips, grids, printing, overview window, palette. 100% implemented in C# as a managed .NET Control. Document/View/Tool architecture with many properties&events. Optional automatic layout.
Dundas Software
Dundas Chart for .NET is the most advanced .NET charting package available today.  With an extremely complete feature set, elegant architecture and easy implementation, Dundas Chart can quickly add advanced Charting functionality to enhance and transform ASP.NET and Windows Forms applications.  Whether you are implementing charting into internal projects, or building applications for clients, Dundas Chart offers advanced technology and advanced results to get the most out of data.
Clickatell's SMS Gateway
Clickatell's Developer Solutions allow you to SMS enable any website or application via a range of API's. Learn More about our API connections.
Free access to .NET Memory Management video
Everything you need to know about Garbage Collection, Temporary Objects, Fragmentation, Finalization and common causes of memory leaks in .NET. Watch the video here.
Microsoft Visual Studio 2010 Professional
Microsoft Visual Studio 2010 Professional will launch on April 12, but you can beat the rush and secure your copy today by pre-ordering at the affordable estimated retail price of $549 (US). Pre-order now.
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.
Developer-Ready ASP.NET 2.0 Web Hosting with 3 MONTHS FREE
Now supporting .NET 3.0 Framework with Windows Workflow Foundation, Windows Communication Foundation (WCF), Windows Presentation Foundation (WPF), windows CardSpace (WCS)! Providing more flexibility for Developers with Web Services Support and a User/Permission Manger. Also supporting MS SQL 2005/2000 with Real-Time Backups, FREE Automated Attach .MDF Tool, FREE SQL Restore and Shrink SQL DB Tools, and SQL
 
   Print Read/Post comments Post a comment  Similar Articles  
   Email to a friend  Bookmark  Author's other articles  
Download Files:
ResourceDictionary in WPF.zip
 
 Post a Feedback, Comment, or Question about this article
Subject:  
Comment:  
Become a Sponsor
 Comments

 Hosted by MaximumASP  |  Found a broken link?  |  Contact Us  |  Terms & conditions  |  Privacy Policy  |  Site Map  |  Suggest an Idea  |  Media Kit
Current Version: 5.2009.6.2
 © 2010  contents copyright of their authors. Rest everything copyright Mindcracker. All rights reserved.