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 Friday, October 26, 2007 4:15 PM by jesseliberty

Comments

# Creating XAML Objects in Code - Jesse Liberty - Silverlight Geek

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

# Creating XAML Objects in Code

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

Friday, October 26, 2007 5:11 PM by Test

# 使用代码创建XAML对象

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

Saturday, October 27, 2007 12:21 PM by lincats

# re: Creating XAML Objects in Code

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

Monday, October 29, 2007 5:49 AM by fkruesch

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

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

# Suppose | Create a Blog

Pingback from  Suppose | Create a Blog

Saturday, March 22, 2008 5:32 AM by Suppose | Create a Blog

# http://silverlight.net/blogs/jesseliberty/archive/2007/10/26 &#8230; | Create a Blog

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

# php code and scripts &raquo; Blog Archive &raquo; http://silverlight.net/blogs/jesseliberty/archive/2007/10/26 &#8230;

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

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

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 &#8230; | My Geek Solutions

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

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

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

Pingback from  Suppose | Create a Blog | Create a Blog

Saturday, March 22, 2008 7:45 AM by Suppose | Create a Blog | Create a Blog

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

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

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

Saturday, March 22, 2008 8:45 AM by Suppose | Create a Blog | Create a Blog | Create a Blog

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

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

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

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 &#8230; | Create a Blog

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

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

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

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 &raquo; Blog Archive &raquo; php code and scripts ?? Blog Archive ?? http://silverlight.net &#8230;

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

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

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

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

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 &#8230; | Create a Blog

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 &#8230; | Create a Blog

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 &#8230; | Create a Blog

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

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

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 &raquo; Blog Archive &raquo; php code and scripts ?? Blog Archive ?? Blog Jocky ?? Blog Archive &#8230;

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 &raquo; Blog Archive &raquo; php code and scripts ?? Blog Archive ?? php code and scripts &#8230;

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 &#8230; | My Geek Solutions

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