Page view counter

Silverlight Tips of the Day - Blog by Mike Snow

Game Programming with Silverlight

Silverlight Tip of the Day #32: How to Declare a Custom User Control from a XAML Page.

UserControls are a great way to separate objects into smaller, more manageable chunks of logic. These controls can reused by different applications and are independent from other controls. Each UserControl can contain any amount of content and logic and can be directly added to your Canvas tree (I.e. MyCanvas.Children.Add(myControl)).

Once you have a UserControl created, how do you reference or declare it from another XAML file (such as Page.xaml)? Doing this is relatively straight forward and we will demonstrate how to do this in this tutorial.

Run this application here to preview: http://silverlight.services.live.com/invoke/66033/Custom%20UserControl/iframe.html

To start, let’s create a custom UserControl called “Card”. In Visual Studio 2008, Right click on your Silverlight Application and choose “Add->New Item…”.

image

In the Add New Item dialog, choose “Silverlight User Control” and change the name to “Card.xaml”.

image

Add the following two images to your Silverlight application project:

CardDiamond3.png                           CardHeartAce.png

image          image

Now, open up Card.xaml and:

  1. Add an Image in the UserContol.
  2. Set the “x:Name” of the Image to “CardImg”.
  3. Set the source default value to”CardHeartAce.png”
  4. Remove the controls Width, Height and Grid tags as they are not needed. Your Card.xaml should now look like this:
<UserControl x:Class="UserCtrl.Card"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <Image x:Name="CardImg" Source="CardHeartAce.png"></Image>
</UserControl>

Next we want to give users the ability to change the image of the card. Open Card.xaml.cs and add the following code which will allow the user to change the image:

namespace UserCtrl
{
    public partial class Card : UserControl
    {
        public Card()
        {
            InitializeComponent();
        }
 
        public ImageSource CardImage
        {
            get { return CardImg.Source; }
            set
            {
                this.CardImg.Source = value;
            }
        }
    }
}

Our final step is to show you how to reference this UserControl from a xaml file such as Page.xaml.

In Page.xaml:

  1. Add a local reference to your Page.xaml UserControl which will allow you to reference objects in your assembly:

    <
    UserControl x:Class="UserCtrl.Page"
        xmlns=http://schemas.microsoft.com/winfx/2006/xaml/presentation 
        xmlns:x=http://schemas.microsoft.com/winfx/2006/xaml 
        xmlns:local="clr-namespace:UserCtrl;assembly=UserCtrl" 
        Width="480" Height="300" >
  2. Declare a couple references to your Card control like this:

    <UserControl x:Class="UserCtrl.Page"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
        xmlns:local="clr-namespace:UserCtrl;assembly=UserCtrl" 
        Width="480" Height="300" >
        <Canvas x:Name="MyCanvas" Background="Gray">
     
            <local:Card Canvas.Left="50" Canvas.Top="20" x:Name="MyCard" CardImage="CardDiamond3.png" >
            </local:Card>
     
            <local:Card Canvas.Left="250" Canvas.Top="20" x:Name="MyCard2" CardImage="CardHeartAce.png" >
            </local:Card>
     
        </Canvas>
    </UserControl>
  3. Run the application and you will see two cards are shown!

    image 

Thank you,
--Mike Snow

 Subscribe in a reader

Comments

Microsoft Weblogs said:

UserControls are a great way to separate objects into smaller, more manageable chunks of logic. These

# August 26, 2008 12:59 PM

Visual Web Developer Team Blog said:

Silverlight Tip of the Day #32 &#160; Title : How to Declare a Custom User Control from a XAML Page.

# August 26, 2008 1:13 PM

News said:

Silverlight Tip of the Day #32 Title : How to Declare a Custom User Control from a XAML Page. Demo :

# August 26, 2008 2:56 PM

Community Blogs said:

Scott Barnes on Submitting SL Bugs, Shawn Wildermuth on SL Firestarter in NYC, Jason Cooke Templatinging

# August 26, 2008 8:35 PM

lixin123 said:

Can the Silverlight guys make it easier to add the controls's reference into the application project by drag and drop or popup menu?

# August 27, 2008 2:57 AM

Silverlight news for August 27, 2008 said:

Pingback from  Silverlight news for August 27, 2008

# August 27, 2008 3:35 AM

Dew Drop - August 27, 2008 | Alvin Ashcraft's Morning Dew said:

Pingback from  Dew Drop - August 27, 2008 | Alvin Ashcraft's Morning Dew

# August 27, 2008 8:50 AM

Solibulo said:

Silverlight 2 basic performance test for procedural animations

# November 5, 2008 4:34 PM

donmarais said:

what about adding namespace of another xaml page in your project to a seperate xaml page so that you can reference a grid on the first xaml page?

# November 6, 2008 10:16 AM

Silverlight Tips of the Day - Blog by Mike Snow said:

The purpose of this post is to create an outline summary all the blogs from my Silverlight tips of the

# January 2, 2009 5:56 PM

o UAU nosso de cada dia said:

essa lista eu copiei desse blog bárbaro (acompanhe por RSS você também): uma lista de dicas super úteis

# January 3, 2009 6:25 AM