Page view counter

Creating XAML Objects in Code

One of the advantages that Silverlight 1.1 does bring to the party is the the isomorphism between XAML and CLR object that is found in Windows Presentation Framework (WPF). Thus, you can create a rectangle declaratively,

<Rectangle x:Name="myRect" Width="120" Height="50" Canvas.Left="10"
              Canvas.Top="10" StrokeThickness="2" Stroke="Black"  >
    <Rectangle.Fill>
      <LinearGradientBrush>
        <GradientStop Color="Red" Offset="0.0"/>
        <GradientStop Color="Orange" Offset="0.2"/>
        <GradientStop Color="Yellow" Offset="0.4"/>
        <GradientStop Color="Green" Offset="0.6"/>
        <GradientStop Color="Blue" Offset="0.8"/>
        <GradientStop Color="Purple" Offset="1.0"/>
      </LinearGradientBrush>
    </Rectangle.Fill>
  </Rectangle>

or you can create the same object programatically,

// declare the rectangle
Rectangle myRectangle = new Rectangle();

 // add it to the canvas  
parentCanvas.Children.Add(myRectangle); 

// set properties
myRectangle.Width = 120;  
myRectangle.Height = 50;
//... 

// set dependency properties
myRectangle.SetValue(LeftProperty, 150);  
myRectangle.SetValue(TopProperty, 10);

// create a Linear Gradient Brush
LinearGradientBrush lgb = new LinearGradientBrush();

// Create a series of GraidentStop objects
GradientStop gs1 = new GradientStop();  
GradientStop gs2 = new GradientStop();
// ...

// Set the color for each stop object
gs1.Color = Colors.Red;
gs2.Color = Colors.Orange;
//...
// Set the offset for each stop object
gs1.Offset = 0.0;
gs2.Offset = 0.2;
// ....

// Add the Gradient stops to the Linear Gradient Brush
lgb.GradientStops.Add(gs1);
lgb.GradientStops.Add(gs2);
//...

// Set the Fill property of MyRectangle to the LinearGradientBrush object
myRectangle.Fill = lgb;

The advantage of being able to create the object programmatically of course is that it makes it much easier to create objects dynamically, in response to events that arise while your program is running.  In  my latest video I explore this aspect of the relationship between XAML and managed code and more.

Published 26 October 2007 04:15 PM by jesseliberty

Comments

# Creating XAML Objects in Code - Jesse Liberty - Silverlight Geek said on 26 October, 2007 04:44 PM

Pingback from  Creating XAML Objects in Code - Jesse Liberty - Silverlight Geek

# Test said on 26 October, 2007 05:11 PM

One of the advantages that Silverlight 1.1 does bring to the party is the the isomorphism between XAML

# lincats said on 27 October, 2007 12:21 PM

使用代码创建XAML对象 引自:silverlight.net/.../creating-xaml-object...

# fkruesch said on 29 October, 2007 05:49 AM

You could shorten some of the stuff using object initializers, e.g.:

Rectangle r = new Rectangle

{

   Width = 120,

   Height = 50,

   Stroke = new SolidColorBrush(Colors.Black),

   StrokeThickness = 2

};

GradientStop gs1 = new GradientStop

{

   Color = Colors.Purple,

   Offset = 0.1

};

cheers

Florian

# SilverSpud » Blog Archive » My Favorite Blogs - Last 5 Posts as of 03 Nov 2007 said on 03 November, 2007 09:47 AM

Pingback from  SilverSpud  &raquo; Blog Archive   &raquo; My Favorite Blogs - Last 5 Posts as of 03 Nov 2007

# http://silverlight.net/blogs/jesseliberty/archive/2007/10/26/creating-xaml-objects-in-code.aspx said on 22 March, 2008 02:55 AM

Pingback from  silverlight.net/.../creating-xaml-objects-in-code.aspx

# Suppose | Create a Blog said on 22 March, 2008 05:32 AM

Pingback from  Suppose | Create a Blog

# http://silverlight.net/blogs/jesseliberty/archive/2007/10/26 … | Create a Blog said on 22 March, 2008 05:32 AM

Pingback from  silverlight.net/.../26 &#8230; | Create a Blog

# php code and scripts » Blog Archive » http://silverlight.net/blogs/jesseliberty/archive/2007/10/26 … said on 22 March, 2008 05:41 AM

Pingback from  php code and scripts  &raquo; Blog Archive   &raquo; silverlight.net/.../26 &#8230;

# php code and scripts » Blog Archive » php code and scripts ?? Blog Archive ?? http://silverlight.net/blogs … said on 22 March, 2008 06:48 AM

Pingback from  php code and scripts  &raquo; Blog Archive   &raquo; php code and scripts ?? Blog Archive ?? http://silverlight.net/blogs &#8230;

# php code and scripts ?? Blog Archive ?? http://silverlight.net/blogs … | My Geek Solutions said on 22 March, 2008 06:52 AM

Pingback from  php code and scripts ?? Blog Archive ?? http://silverlight.net/blogs &#8230; | My Geek Solutions

# Blog Jocky » Blog Archive » php code and scripts ?? Blog Archive ?? http://silverlight.net/blogs … said on 22 March, 2008 07:16 AM

Pingback from  Blog Jocky  &raquo; Blog Archive   &raquo; php code and scripts ?? Blog Archive ?? http://silverlight.net/blogs &#8230;

# Suppose | Create a Blog | Create a Blog said on 22 March, 2008 07:45 AM

Pingback from  Suppose | Create a Blog | Create a Blog

# php code and scripts ?? Blog Archive ?? http://silverlight.net/blogs … | Create a Blog said on 22 March, 2008 07:45 AM

Pingback from  php code and scripts ?? Blog Archive ?? http://silverlight.net/blogs &#8230; | Create a Blog

# Suppose | Create a Blog | Create a Blog | Create a Blog said on 22 March, 2008 08:45 AM

Pingback from  Suppose | Create a Blog | Create a Blog | Create a Blog

# php code and scripts ?? Blog Archive ?? http://silverlight.net … | Create a Blog said on 22 March, 2008 08:45 AM

Pingback from  php code and scripts ?? Blog Archive ?? http://silverlight.net &#8230; | Create a Blog

# php code and scripts ?? Blog Archive ?? http://silverlight.net … | My Geek Solutions said on 22 March, 2008 08:47 AM

Pingback from  php code and scripts ?? Blog Archive ?? http://silverlight.net &#8230; | My Geek Solutions

# php code and scripts ?? Blog Archive ?? php code and scripts ?? Blog … | Create a Blog said on 22 March, 2008 08:48 AM

Pingback from  php code and scripts ?? Blog Archive ?? php code and scripts ?? Blog &#8230; | Create a Blog

# Suppose | Create a Blog | Create a Blog | My Geek Solutions said on 22 March, 2008 08:51 AM

Pingback from  Suppose | Create a Blog | Create a Blog | My Geek Solutions

# php code and scripts » Blog Archive » Blog Jocky ?? Blog Archive ?? php code and scripts ?? Blog Archive … said on 22 March, 2008 09:04 AM

Pingback from  php code and scripts  &raquo; Blog Archive   &raquo; Blog Jocky ?? Blog Archive ?? php code and scripts ?? Blog Archive &#8230;

# php code and scripts » Blog Archive » php code and scripts ?? Blog Archive ?? http://silverlight.net … said on 22 March, 2008 09:06 AM

Pingback from  php code and scripts  &raquo; Blog Archive   &raquo; php code and scripts ?? Blog Archive ?? http://silverlight.net &#8230;

# php code and scripts » Blog Archive » php code and scripts ?? Blog Archive ?? php code and scripts ?? Blog … said on 22 March, 2008 09:06 AM

Pingback from  php code and scripts  &raquo; Blog Archive   &raquo; php code and scripts ?? Blog Archive ?? php code and scripts ?? Blog &#8230;

# Suppose | Create a Blog | Create a Blog | My Geek Solutions | My Geek Solutions said on 22 March, 2008 10:40 AM

Pingback from  Suppose | Create a Blog | Create a Blog | My Geek Solutions | My Geek Solutions

# Suppose | Create a Blog | Create a Blog | My Geek Solutions | Create a Blog said on 22 March, 2008 11:11 AM

Pingback from  Suppose | Create a Blog | Create a Blog | My Geek Solutions | Create a Blog

# php code and scripts ?? Blog Archive ?? php code and scripts … | Create a Blog said on 22 March, 2008 11:12 AM

Pingback from  php code and scripts ?? Blog Archive ?? php code and scripts &#8230; | Create a Blog

# Blog Jocky ?? Blog Archive ?? php code and scripts ?? Blog Archive … | Create a Blog said on 22 March, 2008 11:12 AM

Pingback from  Blog Jocky ?? Blog Archive ?? php code and scripts ?? Blog Archive &#8230; | Create a Blog

# php code and scripts ?? Blog Archive ?? Blog Jocky ?? Blog Archive … | Create a Blog said on 22 March, 2008 11:12 AM

Pingback from  php code and scripts ?? Blog Archive ?? Blog Jocky ?? Blog Archive &#8230; | Create a Blog

# php code and scripts » Blog Archive » Blog Jocky ?? Blog Archive ?? php code and scripts ?? Blog Archive … said on 22 March, 2008 11:17 AM

Pingback from  php code and scripts  &raquo; Blog Archive   &raquo; Blog Jocky ?? Blog Archive ?? php code and scripts ?? Blog Archive &#8230;

# php code and scripts » Blog Archive » php code and scripts ?? Blog Archive ?? Blog Jocky ?? Blog Archive … said on 22 March, 2008 11:17 AM

Pingback from  php code and scripts  &raquo; Blog Archive   &raquo; php code and scripts ?? Blog Archive ?? Blog Jocky ?? Blog Archive &#8230;

# php code and scripts » Blog Archive » php code and scripts ?? Blog Archive ?? php code and scripts … said on 22 March, 2008 11:17 AM

Pingback from  php code and scripts  &raquo; Blog Archive   &raquo; php code and scripts ?? Blog Archive ?? php code and scripts &#8230;

# Suppose | Create a Blog | Create a Blog | My Geek Solutions | My … | My Geek Solutions said on 22 March, 2008 12:49 PM

Pingback from  Suppose | Create a Blog | Create a Blog | My Geek Solutions | My &#8230; | My Geek Solutions