Blue Theme Orange Theme Green Theme Red Theme
 
6 Months Free & No Setup Fees ASP.NET Hosting!
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
6 Months Free & No Setup Fees ASP.NET Hosting!
Search :       Advanced Search »
Home » XAML » Resource Dictionary in Silverlight 3 Using Blend 3

Resource Dictionary in Silverlight 3 Using Blend 3

In this article we will see how we can manage Styles in different files. Resource Dictionary is a cool feature available in Blend 3, where we can store our defined styles.

Author Rank :
Page Views : 13639
Downloads : 90
Rating :
 Rate it
Level : Advanced
   Print Read/Post comments Post a comment  Similar Articles  
   Email to a friend  Bookmark  Author's other articles  
Download Files:
ResourceDictionaryInSL3.zip
 
 
Team Foundation Server Hosting
Become a Sponsor
 Tag Cloud
 Latest Jobs
More ... 
 Latest Interview Questions
More ... 


Introduction

In this article we will see how we can manage Styles in different files. Resource Dictionary is a cool feature available in Blend 3, where we can store our defined styles.

Crating Silverlight Project

Fire up Blend 3 and create a Silverlight Application. Name it as ResourceDictionaryInSL3.

image1.gif

We will do the simple thing, customizing a Button Control.

Add a Button to the Application. Right Click on it and choose Edit Template and then choose Edit a copy.

image2.gif

When you choose Edit a Copy a new dialog box pops up saying Create Style Resource.

First of all Name the Style for Example "MyButtonStyle".

Then you have Define option under that we have three choices to make to store the Style information.

image3.gif

The default is under This Document which will store under the Page's xaml code behind.

The Application option will store the Style information in App.xaml file.

The third option is grayed, as there is no Resource Dictionary. We will use the Resource Dictionary option.

Create a new Resource Dictionary by clicking New.

image4.gif

It asks for giving a name to the file. You can see the extension is xaml.

Now the Resource Dictionary option will be activated with proper file name.

image5.gif

Press ok to continue with our work.

You can see a new file named ResourceDictionary2.xaml added to the Project. As it has only Style information so there is no code behind for it.

image6.gif

I have done simple change to the Button. (I have changed the Background Gradient to Red)

image7.gif

image8.gif

Now we have finished editing the Template, exit editing template.

Open App.xaml in xaml code behind you will find the Resource Dictionary is added to the Application Resource.

<Application
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            x:Class="ResourceDictionaryInSL3.App">
            <Application.Resources>
                        <!-- Resources scoped at the Application level should be defined here. -->
                        <ResourceDictionary>
                                    <ResourceDictionary.MergedDictionaries>
                                                <ResourceDictionary Source="ResourceDictionary1.xaml"/>
                                    </ResourceDictionary.MergedDictionaries>
                        </ResourceDictionary>
            </Application.Resources>
</
Application>

If you see the Button Control in xaml code behind, you will find the Style is Bound to the Name you have given for the Style.

<Button HorizontalAlignment="Left" VerticalAlignment="Top" Width="75" Content="Button" Style="{StaticResource MyButtonStyle}" Margin="198,29,0,0"/>

Now if you open the Resource Dictionary in xaml code behind you will find the Styles you have just changed.

<ResourceDictionary
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
            <Style x:Key="MyButtonStyle" TargetType="Button">
            <Setter Property="Background" Value="#FF1F3B53"/>
                        <Setter Property="Foreground" Value="#FF000000"/>
                        <Setter Property="Padding" Value="3"/>
                        <Setter Property="BorderThickness" Value="1"/>
                        <Setter Property="BorderBrush">
                                    <Setter.Value>
                                                <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                                                            <GradientStop Color="#FFA3AEB9" Offset="0"/>
                                                            <GradientStop Color="#FF8399A9" Offset="0.375"/>
                                                            <GradientStop Color="#FF718597" Offset="0.375"/>
                                                            <GradientStop Color="#FF617584" Offset="1"/>
                                                </LinearGradientBrush>
                                    </Setter.Value>
                        </Setter>
                        <Setter Property="Template">
                                    <Setter.Value>
                                    <ControlTemplate TargetType="Button">
                                    <Grid>
                                    <VisualStateManager.VisualStateGroups>
                                                <VisualStateGroup x:Name="CommonStates">
                                                <VisualState x:Name="Normal"/>
                                    <VisualState x:Name="MouseOver">
                        <Storyboard>
            <DoubleAnimationUsingKeyFrames Storyboard.TargetName="BackgroundAnimation" Storyboard.TargetProperty="Opacity">
                                    <SplineDoubleKeyFrame KeyTime="0" Value="1"/>
            </DoubleAnimationUsingKeyFrames>
                                                                                                                        <ColorAnimationUsingKeyFrames Storyboard.TargetName="BackgroundGradient" Storyboard.TargetProperty="(Rectangle.Fill).(GradientBrush.GradientStops)[1].(GradientStop.Color)">
            <SplineColorKeyFrame KeyTime="0" Value="#F2FFFFFF"/>
            </ColorAnimationUsingKeyFrames>
                                                                                                                        <ColorAnimationUsingKeyFrames Storyboard.TargetName="BackgroundGradient" Storyboard.TargetProperty="(Rectangle.Fill).(GradientBrush.GradientStops)[2].(GradientStop.Color)">
            <SplineColorKeyFrame KeyTime="0" Value="#CCFFFFFF"/>
            </ColorAnimationUsingKeyFrames>
                                                                                                                        <ColorAnimationUsingKeyFrames Storyboard.TargetName="BackgroundGradient" Storyboard.TargetProperty="(Rectangle.Fill).(GradientBrush.GradientStops)[3].(GradientStop.Color)">
            <SplineColorKeyFrame KeyTime="0" Value="#7FFFFFFF"/>
            </ColorAnimationUsingKeyFrames>
            </Storyboard>
            </VisualState>
            <VisualState x:Name="Pressed">
            <Storyboard>
                                                                                                                        <ColorAnimationUsingKeyFrames Storyboard.TargetName="Background" Storyboard.TargetProperty="(Border.Background).(SolidColorBrush.Color)">
            <SplineColorKeyFrame KeyTime="0" Value="#FF6DBDD1"/>
            </ColorAnimationUsingKeyFrames>
                                                                                                                        <DoubleAnimationUsingKeyFrames Storyboard.TargetName="BackgroundAnimation" Storyboard.TargetProperty="Opacity">
            <SplineDoubleKeyFrame KeyTime="0" Value="1"/>
            </DoubleAnimationUsingKeyFrames>
                                                                                                                        <ColorAnimationUsingKeyFrames Storyboard.TargetName="BackgroundGradient" Storyboard.TargetProperty="(Rectangle.Fill).(GradientBrush.GradientStops)[0].(GradientStop.Color)">
            <SplineColorKeyFrame KeyTime="0" Value="#D8FFFFFF"/>
            </ColorAnimationUsingKeyFrames>
                                                                                                                        <ColorAnimationUsingKeyFrames Storyboard.TargetName="BackgroundGradient" Storyboard.TargetProperty="(Rectangle.Fill).(GradientBrush.GradientStops)[1].(GradientStop.Color)">
            <SplineColorKeyFrame KeyTime="0" Value="#C6FFFFFF"/>
            </ColorAnimationUsingKeyFrames>
                                                                                                                        <ColorAnimationUsingKeyFrames Storyboard.TargetName="BackgroundGradient" Storyboard.TargetProperty="(Rectangle.Fill).(GradientBrush.GradientStops)[2].(GradientStop.Color)">
            <SplineColorKeyFrame KeyTime="0" Value="#8CFFFFFF"/>
            </ColorAnimationUsingKeyFrames>
                                                                                                                        <ColorAnimationUsingKeyFrames Storyboard.TargetName="BackgroundGradient" Storyboard.TargetProperty="(Rectangle.Fill).(GradientBrush.GradientStops)[3].(GradientStop.Color)">
            <SplineColorKeyFrame KeyTime="0" Value="#3FFFFFFF"/>
            </ColorAnimationUsingKeyFrames>
            </Storyboard>
            </VisualState>
            <VisualState x:Name="Disabled">
            <Storyboard>
                                                                                                                        <DoubleAnimationUsingKeyFrames Storyboard.TargetName="DisabledVisualElement" Storyboard.TargetProperty="Opacity">
            <SplineDoubleKeyFrame KeyTime="0" Value=".55"/>
            </DoubleAnimationUsingKeyFrames>
            </Storyboard>
            </VisualState>
            </VisualStateGroup>
            <VisualStateGroup x:Name="FocusStates">
            <VisualState x:Name="Focused">
            <Storyboard>
                                                                                                                        <DoubleAnimationUsingKeyFrames Storyboard.TargetName="FocusVisualElement" Storyboard.TargetProperty="Opacity">
            <SplineDoubleKeyFrame KeyTime="0" Value="1"/>
            </DoubleAnimationUsingKeyFrames>
            </Storyboard>
            </VisualState>
            <VisualState x:Name="Unfocused"/>
            </VisualStateGroup>
            </VisualStateManager.VisualStateGroups>
            <Border x:Name="Background" Background="White" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" CornerRadius="3">
            <Grid Margin="1" Background="{TemplateBinding Background}">
            <Border x:Name="BackgroundAnimation" Opacity="0" Background="#FF448DCA"/>
                        <Rectangle x:Name="BackgroundGradient">
                                    <Rectangle.Fill>
            <LinearGradientBrush EndPoint=".7,1" StartPoint=".7,0">
                        <GradientStop Color="#FFFFFFFF" Offset="0"/>
                        <GradientStop Color="#F9FFFFFF" Offset="0.375"/>
                        <GradientStop Color="#E5FFFFFF" Offset="0.625"/>
                        <GradientStop Color="#C6DA0A0A" Offset="1"/>
            </LinearGradientBrush>
            </Rectangle.Fill>
            </Rectangle>
            </Grid>
            </Border>
            <ContentPresenter x:Name="contentPresenter" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="{TemplateBinding Padding}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}" Content="{TemplateBinding Content}" ContentTemplate="{TemplateBinding ContentTemplate}"/>
            <Rectangle x:Name="DisabledVisualElement" Fill="#FFFFFFFF" RadiusX="3" RadiusY="3" IsHitTestVisible="false" Opacity="0"/>
            <Rectangle x:Name="FocusVisualElement" Stroke="#FF6DBDD1" StrokeThickness="1" RadiusX="2" RadiusY="2" Margin="1" IsHitTestVisible="false" Opacity="0"/>
            </Grid>
            </ControlTemplate>
            </Setter.Value>
            </Setter>
            </Style>
            <!-- Resource dictionary entries should be defined here. -->
</ResourceDictionary>

That's it you have successfully used Resource Dictionary In Silverlight 3 using Blend 3.

Enjoy Coding.

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
 
Diptimaya Patra

Diptimaya is working as a Sr. Software Engineer in Microsoft Technologies (C#). He is a Microsoft MVP in Client App Dev, he has a good hands on in Silverlight 2/3/4, WPF 3/4, Expression Blend 3/4.


Follow him in Twitter: http://www.twitter.com/dpatra

Blog: http://dpatra.blogspot.com , http://diptimayapatra.wordpress.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.
Discover the top 5 tips for understanding .NET
Ricky Leeks presents the top 5 tips for understanding .NET Interoperability. 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:
Nevron Chart
Become a Sponsor
 Comments
Thanks by Srividhya On July 29, 2009

Thanks for the article.. i tried using this for the glassy style buttton that you posted last week. I used the resource directory for other buttons. I worked fine except for the color. for the original button i gave blue color but while using the resource directory the button was in yellow color.. do i need to change any specification??

One more query reg treeview. I have created a treeview and wanted to display a datagrid according when the selected item gets changed.
but i am unable to get the selected value.
Treeview.selecteditem and treeview.selectedvalue is not working.
Just let me know if you can help me on that.

Thanks
Srividhya

Reply | Email | Modify 
Nevron Chart
 © 2012  contents copyright of their authors. Rest everything copyright Mindcracker. All rights reserved.