XML
  Home arrow XML arrow Properties and More in XAML
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Silverlight  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
ASP Web Hosting  
ASP.NET Web Hosting 
Windows Web Hosting
 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
XML

Properties and More in XAML
By: O'Reilly Media
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 1
    2007-04-12

    Table of Contents:
  • Properties and More in XAML
  • Attached Properties
  • Binding Properties
  • Codebehind

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Properties and More in XAML


    (Page 1 of 4 )

    In this third part of a three-part series covering the basics of XAML, you will learn about attached properties, binding properties, and more. It is excerpted from the book XAML in a Nutshell, written by Lori A. MacVittie (O'Reilly, 2006; ISBN: 596526733). Copyright © 2006 O'Reilly Media, Inc. All rights reserved. Used with permission from the publisher. Available from booksellers or direct from O'Reilly Media.

    Abbreviated Syntax

    Abbreviated syntax must sound like jabberwocky at this point, but it’s really a pretty neat concept. It uses a predefined format, such as CSV (comma-separated values), to essentially declare the arguments that will be passed to the appropriate class constructor beneath the covers. You can think of the String definition as the list of arguments you’d normally pass to a constructor, except that sometimes you don’t need a comma to separate the arguments.

    Example 3-11 first creates an EllipseGeometry in C# and then assigns a Point to be its Center property by instantiating a new Point and passing the appropriate values to its constructor. The XAML code in the same example creates an EllipseGeometry and then assigns a Point to be its Center attribute using abbrevi ated syntax. The 0.5, 0.5 is parsed by the WPF engine, and the values are passed to a Point constructor as its arguments.

    Example 3-11. Abbreviated syntax and arguments in C#

    C #
    EllipseGeometry ellipse;
    ellipse.Center=new Point(0.5, 0.5);

    XAML
    <EllipseGeometry
       
    Center="0.5,0.5" />

    A very common example of using abbreviated syntax to declare attribute values is the assignment of predefined color names to an attribute declared as type Brush , such as the Background attribute. Rather than forcing you to go through all the typing required to explicitly declare a Brush and set its color, XAML allows you to just declare the attribute as Red or Green instead.

    Figure 3-3 shows the result of declaring a Red SolidColorBrush as the background Brush for a Button using both abbreviated markup and by explicitly declaring the complex attribute. Example 3-12 shows the code used to declare both elements.


    Figure 3-3.   Result of explicit declaration and abbreviated markup declaration of a Brush attribute on a Button

    Example 3-12. Explicit declaration of a Brush versus abbreviated markup

    <StackPanel
        xmlns=http://schemas.microsoft.com/ winfx/avalon/2005
        Margin="10 10 10 10">
        <Button
           
    Width="350"
            Height="30"
            Content="Button with explicitly declared Background Brush">
            <Button.Background>

                <SolidColorBrush Color="Red" />
           
    </Button.Background>
        </Button>
        <Button
            Width="350"
            Height="30"
            Background="Red"
            Content="Button with a Background Brush declared using abbreviate
                   
    markup"/>
    </StackPanel>

    As you can see in Figure 3-3, both buttons are painted with the same back ground, regardless of the method used to declare the Brush . Abbreviated syntax is typically used because it requires less typing. There are no advantages to using explicit syntax in most cases where abbreviated syntax is available, and it’s less typing for you.

    A more complex example is the common use of abbreviated markup syntax to declare elements of the type Point . Point is a common, complex attribute that is used in the declaration of almost every geometric XAML element. You can use the abbreviated markup syntax for a Point element wherever an element of type Point is declared. You’ll notice in Example 3-13 that EllipseGeometry has several attributes. While RadiusX and RadiusY are Double values, the Center attribute for an EllipseGeometry is actually a complex attribute of type Point . In its abbreviated syntax, Point accepts two comma-separated values representing the X and Y positions, respectively. Example 3-13 shows different ways of using Point (in this case, it is used through the Center attribute).

    Example 3-13. Example of abbreviated markup versus explicit syntax

    <GeometryGroup>
       
    <EllipseGeometr y
            RadiusX="0.45"
            RadiusY="0.2"
            Center="0.5,0.5" />

        <EllipseGeometry
            RadiusX="0.2"
            RadiusY="0.45">  
            <EllipseGeometry.Center>
                <Point
                    X="0.5"
                    Y="0.5" />

            </EllipseGeometry.Center>
         </EllipseGeometry>
    </GeometryGroup>

    Elements that can be declared using abbreviated markup syntax are specifically noted in Part III.

    More XML Articles
    More By O'Reilly Media


       · This article is an excerpt from the book "XAML in a Nutshell," published by...
     

    Buy this book now. This article is excerpted from the book XAML in a Nutshell, written by Lori A. MacVittie (O'Reilly, 2006; ISBN: 596526733). Check it out today at your favorite bookstore. Buy this book now.

    XML ARTICLES

    - More on Triggers and Styles and Control Temp...
    - Looking at Triggers with Styles and Control ...
    - A Closer Look at Styles and Control Templates
    - Styles and Control Templates
    - Properties and More in XAML
    - Elements and Attributes in XAML
    - XAML in a Nutshell
    - Importing XML Files into Access 2007
    - Using MSXML3.0 with VB 6.0
    - MSXML, concluded
    - MSXML, continued
    - MSXML Tutorial
    - Generating XML Schema Dynamically Using VB.N...
    - XSL Transformations using ASP.NET
    - Applying XSLT to XML Using ASP.NET





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 2 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek