Recommended

Skip Navigation LinksHome / Tips / View Tip

Tip: How to declare static resources in the scope of a Silverlight user control?

+ Add to SilverlightShow Favorites
0 comments   /   posted by Denislav Savkov on Sep 02, 2008
(6 votes)
Categories: Resources

User control static resources are used to declare instances of objects that should be alive and reusable throughout the whole life of a user control. When you declare a static resource in the Xaml of a user control Visual Studio actually declares this instance as an internal class field of that user control, thus making it directly accessible only in your code behind. Usually such things like styles and templates are declared as static resources and as result they can be used multiple times from within your control.

Xaml

   <UserControl x:Class="SnackBites.MySilverlightControl"
       xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
       xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
       Width="400" Height="300">
       <UserControl.Resources>
           <Style x:Name="simpleStyle" TargetType="Button">
               <Setter Property="Content" Value="Static style content"></Setter>
           </Style>
       </UserControl.Resources>
       <Grid x:Name="LayoutRoot" Background="White">
    </Grid>
   </UserControl>
Note that for each resource you declare, either x:Name or x:Key should be specified!
That's it!
Share


Comments

Comments RSS RSS
No comments

Add Comment

 
 

   
  
  
   
Please add 8 and 8 and type the answer here:

Help us make SilverlightShow even better and win a free t-shirt. Whether you'd like to suggest a change in the structure, content organization, section layout or any other aspect of SilverlightShow appearance - we'd love to hear from you! Need a material (article, tutorial, or other) on a specific topic? Let us know and SilverlightShow content authors will work to have that prepared for you. (hide this)