Using XAML in Windows Presentation Foundation

XAML (Extensible Application mark-up Language) is a declarative language. It is similar to ASP.NET 2.0 web page definition using HTML tags. It defines the managed object instantiation. These files have .xaml extension. You can use XAML to write WPF applications. You should understand the elements, the properties, attributes of the elements and other aspects of XAML to create applications.



The Root Element

The root elements for the XAML files are:
1. Page for Web application
2. Window for Windows application
3. ResourceDictionary for an external dictionary
4. Application for application definition

XAML code behind:

XAML code behind:

<Page
x:Class="SampleNameSpace.Class1">
<Button Click="OnOk" >Ok</Button>
</Page>

namespace SampleNameSpace
{
public partial class Class1
{
void OnOk(object sender, RoutedEventArgs e)
{
Button but = e.Source as Button;
but.Background = Brushes.Blue;
}
}
}

The above C# code is defined separately. This file is called code behind file. When compiling the .xaml the compiler also include the code behind file.

Attached properties and attached events

An element can specify any property, even if it does not exist in that element.
Syntax for attached property is,

ownerType.propName = value

Example:

<DockPanel>
<Button DockPanel.Dock="Left" Width="100" Height="20">I am on the left</Button>
<Button DockPanel.Dock="Right" Width="100" Height="20">I am on the right</Button>
</DockPanel>

Attached events use syntax like ownerType.eventName. An example for attached event is Mouse. MouseDown.

But for elements derived from the UIElement or ContentElement, attached event syntax is not must, because the base class creates alias for these important input events.

Syntax and grammar

XAML is case sensitive. The XML loader will drop extra white spaces.

A class might support the usage of it as an element in the XAML file. But it must be placed properly under the page or application elements. For example, MenuItem must be placed inside Menu element.

The content property values must be continuous. Child elements must be continuous.

Typeconverter-Enabled Attribute Values allows values for attribute to be specified in string format. For example, the margin property can be specified as single string. This avoids using lengthy property syntax for defining complex property.

<Button Margin="10,20,10,30" Content="Click me"/>
<Button Content="Click me">
<Button.Margin>
<Thickness Left="10" Top="20" Right="10" Bottom="30"/>
</Button.Margin>
</Button>

Syntax for Attribute

<Button Background="Blue" Foreground="Red" Content="This is a button"/>

Syntax for Property Element

<Button>
<Button.Background>
<SolidColorBrush Color="Green"/>
</Button.Background>
<Button.Foreground>
<SolidColorBrush Color="Yellow"/>
</Button.Foreground>
<Button.Content>
This is a button
</Button.Content>
</Button>

Collection Types and XAML Collection Properties

Object element on a page that is nested as a child element of another element can be
1. member of an implicit collection property of its parent element
2. or an element that specifies the value of the XAML content property for the parent element

Markup Extensions

When an XAML element property is assigned with a reference type value, they have to be specified using either, XAML markup extension or property element syntax. Using markup extension syntax, a reference to an existing object can be made. This reduces the memory usage by your application. Curly braces ({ and }) indicate a markup extension usage in attribute syntax.

You can find a lot of tutorials on building an XAML application in the web. You can use the above concepts to create your own XAML application.

_______________________________________________________________________

Add to My Yahoo!


FREE Subscription

Subscribe to our mailing list and receive new articles
through email. Keep yourself updated with latest
developments in the industry.

Name:
Email:

Note : We never rent, trade, or sell my email lists to
anyone. We assure that your privacy is respected
and protected.

Visit .NET Programming Tutorial Homepage

______________________________________________________

Recommended Resource


| Privacy Policy for www.dotnet-guide.com | Disclosure | Contact |

Copyright - © 2004 - 2017 - All Rights Reserved.