Jesse Liberty - Silverlight Geek

By, For and About Silverlight Developers

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.

Comments

Creating XAML Objects in Code - Jesse Liberty - Silverlight Geek said:

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

# October 26, 2007 4:44 PM

Test said:

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

# October 26, 2007 5:11 PM

lincats said:

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

# October 27, 2007 12:21 PM

hgecom » Creating XAML Objects in Code said:

Pingback from  hgecom &raquo; Creating XAML Objects in Code

# October 27, 2007 8:34 PM

fkruesch said:

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

# October 29, 2007 5:49 AM

SilverSpud » Blog Archive » My Favorite Blogs - Last 5 Posts as of 03 Nov 2007 said:

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

# November 3, 2007 9:47 AM

Blog Jocky » Blog Archive » Suppose said:

Pingback from  Blog Jocky  &raquo; Blog Archive   &raquo; Suppose

# March 22, 2008 5:01 AM

Blog Jocky » Blog Archive » http://silverlight.net/blogs/jesseliberty/archive/2007/10/26 … said:

Pingback from  Blog Jocky  &raquo; Blog Archive   &raquo; silverlight.net/.../26 &#8230;

# March 22, 2008 5:02 AM

Suppose | Create a Blog said:

Pingback from  Suppose | Create a Blog

# March 22, 2008 5:32 AM

http://silverlight.net/blogs/jesseliberty/archive/2007/10/26 … | Create a Blog said:

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

# March 22, 2008 5:32 AM

php code and scripts » Blog Archive » http://silverlight.net/blogs/jesseliberty/archive/2007/10/26 … said:

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

# March 22, 2008 5:41 AM

php code and scripts » Blog Archive » php code and scripts ?? Blog Archive ?? http://silverlight.net/blogs … said:

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

# March 22, 2008 6:48 AM

php code and scripts ?? Blog Archive ?? http://silverlight.net/blogs … | My Geek Solutions said:

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

# March 22, 2008 6:52 AM

Blog Jocky » Blog Archive » php code and scripts ?? Blog Archive ?? http://silverlight.net/blogs … said:

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

# March 22, 2008 7:16 AM

Suppose | Create a Blog | Create a Blog said:

Pingback from  Suppose | Create a Blog | Create a Blog

# March 22, 2008 7:45 AM

php code and scripts ?? Blog Archive ?? http://silverlight.net/blogs … | Create a Blog said:

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

# March 22, 2008 7:45 AM

Suppose | Create a Blog | Create a Blog | Create a Blog said:

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

# March 22, 2008 8:45 AM

php code and scripts ?? Blog Archive ?? http://silverlight.net … | Create a Blog said:

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

# March 22, 2008 8:45 AM

php code and scripts ?? Blog Archive ?? http://silverlight.net … | My Geek Solutions said:

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

# March 22, 2008 8:47 AM

php code and scripts ?? Blog Archive ?? php code and scripts ?? Blog … | Create a Blog said:

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

# March 22, 2008 8:48 AM

Blog Jocky ?? Blog Archive ?? php code and scripts ?? Blog Archive … | Create a Blog said:

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

# March 22, 2008 8:48 AM

Suppose | Create a Blog | Create a Blog | My Geek Solutions said:

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

# March 22, 2008 8:51 AM

php code and scripts » Blog Archive » Blog Jocky ?? Blog Archive ?? php code and scripts ?? Blog Archive … said:

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

# March 22, 2008 9:04 AM

php code and scripts » Blog Archive » php code and scripts ?? Blog Archive ?? http://silverlight.net … said:

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

# March 22, 2008 9:06 AM

php code and scripts » Blog Archive » php code and scripts ?? Blog Archive ?? php code and scripts ?? Blog … said:

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

# March 22, 2008 9:06 AM

Blog Jocky » Blog Archive » php code and scripts ?? Blog Archive ?? php code and scripts ?? Blog … said:

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

# March 22, 2008 9:39 AM

Blog Jocky » Blog Archive » php code and scripts ?? Blog Archive ?? http://silverlight.net … said:

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

# March 22, 2008 10:40 AM

Blog Jocky » Blog Archive » Blog Jocky ?? Blog Archive ?? php code and scripts ?? Blog Archive … said:

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

# March 22, 2008 10:40 AM

Suppose | Create a Blog | Create a Blog | My Geek Solutions | My Geek Solutions said:

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

# March 22, 2008 10:40 AM

Suppose | Create a Blog | Create a Blog | My Geek Solutions | Create a Blog said:

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

# March 22, 2008 11:11 AM

php code and scripts ?? Blog Archive ?? php code and scripts … | Create a Blog said:

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

# March 22, 2008 11:12 AM

Blog Jocky ?? Blog Archive ?? php code and scripts ?? Blog Archive … | Create a Blog said:

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

# March 22, 2008 11:12 AM

php code and scripts ?? Blog Archive ?? Blog Jocky ?? Blog Archive … | Create a Blog said:

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

# March 22, 2008 11:12 AM

php code and scripts » Blog Archive » Blog Jocky ?? Blog Archive ?? php code and scripts ?? Blog Archive … said:

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

# March 22, 2008 11:17 AM

php code and scripts » Blog Archive » php code and scripts ?? Blog Archive ?? Blog Jocky ?? Blog Archive … said:

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

# March 22, 2008 11:17 AM

php code and scripts » Blog Archive » php code and scripts ?? Blog Archive ?? php code and scripts … said:

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

# March 22, 2008 11:17 AM

Suppose | Create a Blog | Create a Blog | My Geek Solutions | My … | My Geek Solutions said:

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

# March 22, 2008 12:49 PM