WinFX: Windows Presentation Foundation (WPF) and XAML - You Can Start Today
page 2 of 3
by Glen Sollors
Feedback
Average Rating: This article has not yet been rated.
Views (Total / Last 10 Days): 21044/ 44

A Simple Example Using XAML

Creating basic XAML and Windows Presentation Foundation (WPF) 2D Graphics is very easy and is similar to the SVG Syntax.  The following sample shows a line, an ellipse, and a rectangle.  Gradients and text could easily be added to these objects as well.

Sample XAML Rendering

Code for the example is shown in Listing 1.

Listing 1:  Simple 2D Graphic Example

<Canvas Background="#FFFFFFFF">
 
<Panel.Children>
   
<Rectangle Fill="#FF0000FF" Height="72" Canvas.Left="74"
     
Stroke="#FF000000" Canvas.Top="77" Width="131" />
   
<Ellipse CenterX="231" CenterY="76" Fill="#FFFF0000" Canvas.Left="0"
     
RadiusX="55" RadiusY="56" Stroke="#FFA52A2A" StrokeThickness="5" Canvas.Top="0" />
    
<Line Canvas.Left="0" Stroke="#FF008000" StrokeThickness="10" Canvas.Top="0"
     
X1="318" X2="96" Y1="46" Y2="211" />
 
</Panel.Children>
</
Canvas>

If you are building UIs that require common controls such as List Box, Toggle, Check Box, and Calendar, WFP has all these and other common WinForm controls.  It is also easy to add and modify controls to meet your personal needs.

A key differentiator in using WPF is the dependency property system.  It is very important that developers understand dependency properties based on the features they offer and ensuring that they are leveraged.   In WPF both regular and attached dependency properties are used to set the attributes of an object. The easiest way to establish and create a dependency property is using XAML.  Dependency properties can be used for elements such as Data Binding, Animation, and Styling.  Properties can also be created easily and directly through WPF.  The developer really does not have to be concerned with anything else that may be hiding. 

Most public properties on WPF objects have a corresponding static dependency property. The dependency property is used as a key in locating the value of the object you are looking for. Below is what a dependency property looks like in the dependency property system in WPF.

Listing 2 Dependency Property System in WPF

public class DependencyObject
{
  public static DependencyProperty dependencyProperty1 =
    DependencyProperty.Register("Property1", typeof(Int), typeof(Button));
 
  public static DependencyProperty dependencyProperty2 =
    DependencyProperty.Register("Proprty2", typeof(String), typeof(Button));
 
  System.Collections.Hashtable hashTable = new
  System.Collections.Hashtable();
 
  public object GetValue(DependencyProperty dp)
  {
    return hashTable[dp];
  }
 
  public void SetValue(DependencyProperty dp, object value)
  {
    return hashTable.Add(dp, value);
  }
}

Unlike regular dependency properties, ‘attached’ dependency properties are static dependency properties that belong to one class but can be used in another.  Below is a XAML code example.

<Rectangle Fill="#FF0101FF" Height="61" Canvas.Left="65" Stroke="#FF000333"
  Canvas.Top
="33" Width="142" />

Regular dependency properties of the Rectangle include Fill, Height, and Stroke. The Canvas.Top property is an attached dependency property. This property belongs to the Canvas class, but is used by the Rectangle class to specify its X and Y position within a canvas. These properties work well with Canvas, Grid, and DockPanel classes in WPF.


View Entire Article

User Comments

Title: Good for first face   
Name: Amit Sinha
Date: 2009-03-25 3:10:24 AM
Comment:
Hello Glen,

Nice..

This article help me undertsand that what is WPF.
This gave me direction to move ahead.

GR8
Title: Good One   
Name: Chetan Deshmukh
Date: 2008-03-14 1:07:31 PM
Comment:
Hello Glen,

I am a .NET 3.5 developer and currently working in WPF. This article is very good for beginner to understand basics of WPF.

Thanks,
Chetan Deshmukh
chetan.deshmukh@e-zest.net
Title: verry user friendly   
Name: samar
Date: 2007-04-04 12:12:59 AM
Comment:
It looking very usefull and good user friendly.If MS find and replace the coding for mostof the user needs then it will be the only top most applycation in the world.
Title: Looks promising   
Name: kf
Date: 2007-03-16 1:40:25 PM
Comment:
Very interesting. MS definitely seems to be going in the right direction with their new foundations and SOA approach. The one comment that just kills me here is

"Since there are other smart Open Source mainframes out there I would not recommend code that is only running on one operating system."

I know this is highly debatable but I'm looking for a platform that allows me to do anything a custom needs and products work well together. This open source crap sounds great for developers but is a nightmare in a corporate environment.

Keep going MS, set a vision and align your products that allow us to easily delight our customers. Those that want the open source so you can hodge podge solutions for your customers, use the open source equivalent for WPF. The more developers that provide those types of solutions to their customers the better we look when we go in behind you after the customer has had enough.

For those who are on the fence, a suggestion is to think platforms that work in your customers environment, not solutions that silo information and leave interoperability as a pipe dream.

Good article!
Title: Linux   
Name: Jeroen Haan
Date: 2006-12-02 9:23:40 AM
Comment:
If this is presented as the standard by some near monopolist, what about users of other operating systems?

Since there are other smart Open Source mainframes out there I would not recommend code that is only running on one operating system.

cheers,
Jeroen Haan
Senior Consultant
Title: We need More Details about XAML   
Name: V.Sekar
Date: 2006-08-10 6:07:12 AM
Comment:
it is useful for beginners
Title: how to return a boolean in dependency property   
Name: praveenalwar
Date: 2006-08-10 1:38:42 AM
Comment:
hai,iam registering a "isvertical" property for axis,and i need to find whether the axis is vertical or not.
for that i registered a dp with the name "IsVertical" and of return type"Boolean" ,parent from"Axis".
but iam not able to understand what to return? i mean what code can i write to make sure that the line is vertical and return true for that.please help me sir?
Title: Managing Partner   
Name: Gregory Olinyk
Date: 2006-06-01 11:39:50 PM
Comment:
Herb is always teaching me new things. This looks promising.
Title: Nice   
Name: chandra sekhar.G
Date: 2005-12-09 6:59:06 AM
Comment:
nice one
Title: Goodone   
Name: Vishwanath Kuntala
Date: 2005-12-09 1:09:00 AM
Comment:
Good one for Beginners who wants to know about what is Windows Presentaion Foundation.






Community Advice: ASP | SQL | XML | Regular Expressions | Windows


©Copyright 1998-2021 ASPAlliance.com  |  Page Processed at 2021-04-13 6:43:35 AM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search