Monday, July 13, 2009

XAML PART I

XAML [eXtensible Application Markup Language] is used to write WPF applications. XAML files are xml files that generally have the .XAML extension. Developer can create UI stuffs using XAML markup which is separate from code behind files used for business logics.

e.g.

<Button Content = “ZAMMEL is XAML” />

In the above example Button element object directly relates to Button class of WPF.

When you specify an object element tag, you are instructing the XAML loader to create new instance of the named class when your XAML page is either compiled or interpreted.

Note:

If the user wants any class as an object element in XAML, the class must have a public parameterless constructor.

Property Setting Syntax:

Setting Properties in XAML can be done using a variety of syntaxes. Properties can be provided in the form of attributes also. In the Button example Content is a property which is represented as an xml attribute. All the properties cannot be expressed as attribute. We have another syntax called Property element syntax. Syntax is

e.g.

<Button Content="I am XAML" >
<Button.Background >
<SolidColorBrush Color="Aqua"/>
</Button.Background >
</Button>

Here content is represented as attribute syntax and Background is represented as Property element syntax.

Markup extensions are another property writing syntax which is useful when you want to return existing instance. {&} represents markup extensions.

E.g. Binding Markup extension; StaticResource Markup extension.

<Window.Resources>
<local:TestProp x:Key="tp1" />
</Window.Resources >

<TextBox Height="26" Margin="84,75,108,0" Name="textBox1" VerticalAlignment="Top"
Text="{Binding Path=MyColor,Source={StaticResource tp1}}" />


In this case it returns a reference to the MyColor Property that was previously instantiated as a keyed resource in resource dictionary.

Summary:

XAML files are xml files with .XAML extension we can specify the properties as Attribute Syntax, Property Object Element Syntax and Markup extensions.

No comments:

Post a Comment