XAML Vs C# Code ”; Previous Next You can use XAML to create, initialize, and set the properties of objects. The same activities can also be performed using programming code. XAML is just another simple and easy way to design UI elements. With XAML, it is up to you to decide whether you want to declare objects in XAML or declare them using code. Let’s take a simple example to demonstrate how to write in XAML − <Window x:Class = “XAMLVsCode.MainWindow” xmlns = “http://schemas.microsoft.com/winfx/2006/xaml/presentation” xmlns:x = “http://schemas.microsoft.com/winfx/2006/xaml” Title = “MainWindow” Height = “350” Width = “525”> <StackPanel> <TextBlock Text = “Welcome to XAML Tutorial” Height = “20” Width = “200” Margin = “5”/> <Button Content = “Ok” Height = “20” Width = “60” Margin = “5”/> </StackPanel> </Window> In this example, we have created a stack panel with a Button and a Text block and defined some of the properties of button and text block such as Height, Width, and Margin. When the above code is compiled and executed, it will produce the following output − Now look at the same code which is written in C#. using System; using System.Text; using System.Windows; using System.Windows.Controls; namespace XAMLVsCode { /// <summary> /// Interaction logic for MainWindow.xaml /// </summary> public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); // Create the StackPanel StackPanel stackPanel = new StackPanel(); this.Content = stackPanel; // Create the TextBlock TextBlock textBlock = new TextBlock(); textBlock.Text = “Welcome to XAML Tutorial”; textBlock.Height = 20; textBlock.Width = 200; textBlock.Margin = new Thickness(5); stackPanel.Children.Add(textBlock); // Create the Button Button button = new Button(); button.Content = “OK”; button.Height = 20; button.Width = 50; button.Margin = new Thickness(20); stackPanel.Children.Add(button); } } } When the above code is compiled and executed, it will produce the following output. Note that it is exactly the same as the output of XAML code. Now you can see that how simple it is to use and understand XAML. Print Page Previous Next Advertisements ”;
Category: xaml
XAML – Data Binding
XAML – Data Binding ”; Previous Next Data binding is a mechanism in XAML applications that provides a simple and easy way for Windows Runtime Apps using partial classes to display and interact with data. The management of data is entirely separated from the way the data is displayed in this mechanism. Data binding allows the flow of data between UI elements and data object on user interface. When a binding is established and the data or your business model changes, then it will reflect the updates automatically to the UI elements and vice versa. It is also possible to bind, not to a standard data source, but rather to another element on the page. Data binding can be of two types − One-way data binding Two-way data binding One-Way Data Binding In one-way binding, data is bound from its source (that is the object that holds the data) to its target (that is the object that displays the data). Let’s have a look at a simple example of one-way data binding. The following XAML code creates four text blocks with some properties. <Window x:Class = “DataBindingOneWay.MainWindow” xmlns = “http://schemas.microsoft.com/winfx/2006/xaml/presentation” xmlns:x = “http://schemas.microsoft.com/winfx/2006/xaml” Title = “MainWindow” Height = “350” Width = “604”> <Grid> <StackPanel Name = “Display”> <StackPanel Orientation = “Horizontal” Margin = “50, 50, 0, 0”> <TextBlock Text = “Name: ” Margin = “10” Width = “100” /> <TextBlock Margin = “10” Width = “100” Text = “{Binding Name}” /> </StackPanel> <StackPanel Orientation = “Horizontal” Margin = “50,0,50,0”> <TextBlock Text = “Title: ” Margin = “10” Width = “100” /> <TextBlock Margin = “10” Width = “100” Text = “{Binding Title}” /> </StackPanel> </StackPanel> </Grid> </Window> Text properties of two text blocks are set to “Name” and “Title” statically, while the other two text blocks Text properties are bound to “Name” and “Title” which are class variables of Employee class which is shown below. using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace DataBindingOneWay { public class Employee { public string Name { get; set; } public string Title { get; set; } public static Employee GetEmployee() { var emp = new Employee() { Name = “Ali Ahmed”, Title = “Developer” }; return emp; } } } In this class, we have just two variables, Name and Title, and one static method in which the Employee object is initialized which will return that employee object. So we are binding to a property, Name and Title, but we have not selected what object that property belongs to. The easiest way is to assign an object to DataContext whose properties we are binding in the following C# code − using System; using System.Windows; using System.Windows.Controls; namespace DataBindingOneWay { /// <summary> /// Interaction logic for MainWindow.xaml /// </summary> public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); DataContext = Employee.GetEmployee(); } } } Let”s run this application and you can see immediately in our MainWindow that we have successfully bound to the Name and Title of that Employee object. Two-Way Data Binding In two-way binding, the user can modify the data through the user interface and have that data updated in the source. If the source changes while the user is looking at the view, you would want to update the view. Example Let’s have a look at the following example in which one combobox with three combobox items and one textbox are created with some properties. In this example, we don’t have any standard data source, but the UI elements are bound to other UI elements. <Window x:Class = “XAMLTestBinding.MainWindow” xmlns = “http://schemas.microsoft.com/winfx/2006/xaml/presentation” xmlns:x = “http://schemas.microsoft.com/winfx/2006/xaml” Title = “MainWindow” Height = “350” Width = “604”> <StackPanel> <ComboBox Name = “comboBox” Margin = “50” Width = “100”> <ComboBoxItem Content = “Green” /> <ComboBoxItem Content = “Yellow” IsSelected = “True” /> <ComboBoxItem Content = “Orange” /> </ComboBox> <TextBox Name = “textBox” Margin = “50” Width = “100” Height = “23” VerticalAlignment = “Top” Text = “{Binding ElementName = comboBox, Path = SelectedItem.Content, Mode = TwoWay, UpdateSourceTrigger = PropertyChanged}” Background = “{Binding ElementName = comboBox, Path = SelectedItem.Content}”> </TextBox> </StackPanel> </Window> When you compile and execute the above code, it will produce the following output. When the user selects an item from the combobox, the textbox text and the background color will be updated accordingly. Similarly, when the user types a valid color name in the textbox, then the combobox and the textbox background color will also be updated. Print Page Previous Next Advertisements ”;
XAML – Markup Extensions
XAML – Markup Extensions ”; Previous Next In XAML applications, markup extensions are a method/technique to gain a value that is neither a specific XAML object nor a primitive type. Markup extensions can be defined by opening and closing curly braces and inside that curly braces, the scope of the markup extension is defined. Data binding and static resources are markup extensions. There are some predefined XAML markup extensions in System.xaml which can be used. Let’s have a look at a simple example where StaticResources markup extension is used which is a predefined XAML markup extension. The following XAML code creates two text blocks with some properties and their foreground is defined in Window.Resources. <Window x:Class = “XAMLStaticResourcesMarkupExtension.MainWindow” xmlns = “http://schemas.microsoft.com/winfx/2006/xaml/presentation” xmlns:x = “http://schemas.microsoft.com/winfx/2006/xaml” Title = “MainWindow” Height = “350” Width = “525”> <Window.Resources> <SolidColorBrush Color = “Blue” x:Key = “myBrush”></SolidColorBrush> </Window.Resources> <Grid> <StackPanel Orientation = “Vertical”> <TextBlock Foreground = “{StaticResource myBrush}” Text = “First Name” Width = “100” Margin = “10” /> <TextBlock Foreground = “{StaticResource myBrush}” Text = “Last Name” Width = “100” Margin = “10” /> </StackPanel> </Grid> </Window> In Window.Resources, you can see x:Key is used which uniquely identifies the elements that are created and referenced in an XAML defined dictionary to identify a resource in a resource dictionary. When you compile and execute the above code, it will produce the following MainWindow. You can see the two text blocks with blue foreground color. In XAML, custom markup extensions can also be defined by inheriting MarkupExtension class and overriding the ProvideValue method which is an abstract method in the MarkupExtension class. Let’s have a look at a simple example of custom markup extension. <Window x:Class = “XAMLMarkupExtension.MainWindow” xmlns = “http://schemas.microsoft.com/winfx/2006/xaml/presentation” xmlns:x = “http://schemas.microsoft.com/winfx/2006/xaml” xmlns:my = “clr-namespace:XAMLMarkupExtension” Title = “MainWindow” Height = “350” Width = “525”> <Grid> <Button Content = “{my:MyMarkupExtension FirstStr = Markup, SecondStr = Extension}” Width = “200” Height = “20” /> </Grid> </Window> In the above XAML code, a button is created with some properties and for the content value, a custom markup extension (my:MyMarkupExtension) has been used with two values “Markup” and “Extension” which are assigned to FirstStr and SecondStr respectively. Actually, MyMarkupExtension is a class which is derived from MarkupExtension as shown below in the C# implementation. This class contains two string variables, FirstStr and SecondStr, which are concatenated and return that string from the ProvideValue method to the Content of a button. using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Markup; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; namespace XAMLMarkupExtension { /// <summary> /// Interaction logic for MainWindow.xaml /// </summary> public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); } } public class MyMarkupExtension : MarkupExtension { public MyMarkupExtension() { } public String FirstStr { get; set; } public String SecondStr { get; set; } public override object ProvideValue(IServiceProvider serviceProvider) { return FirstStr + ” ” + SecondStr; } } } Let”s run this application and you can see immediately in our MainWindow that “markup extension” has been successfully used as the content of the button. Print Page Previous Next Advertisements ”;
XAML – Useful Resources
XAML – Useful Resources ”; Previous Next The following resources contain additional information on XAML. Please use them to get more in-depth knowledge on this. Useful Links on XAML XAML Wiki − Wikipedia Reference for XAML. Useful Books on XAML To enlist your site on this page, please drop an email to [email protected] Print Page Previous Next Advertisements ”;
XAML – Layouts
XAML – Layouts ”; Previous Next The layout of controls is very important and critical for application usability. It is required to arrange a group of GUI elements in your application. There are certain important points to consider while selecting layout panels; Positions of the child elements. Sizes of the child elements. Layering of overlapping child elements on top of each other. Fixed pixel arrangement of controls doesn’t work when an application has been used on different screen resolutions. XAML provides a rich set of built-in layout panels to arrange GUI elements in an appropriate way. Some of the most commonly used and popular layout panels are as follows − Sr.No Panels & Description 1 StackPanel Stack panel is a simple and useful layout panel in XAML. In a stack panel, child elements can be arranged in a single line, either horizontally or vertically, based on the orientation property. 2 WrapPanel In WrapPanel, child elements are positioned in a sequential order from left to right or from top to bottom based on the orientation property. 3 DockPanel DockPanel defines an area to arrange child elements relative to each other, either horizontally or vertically. With DockPanel, you can easily dock child elements to top, bottom, right, left, and center with Dock property. 4 CanvasPanel Canvas panel is the basic layout panel in which child elements can be positioned explicitly using coordinates that are relative to the Canvas any side such as left, right, top, and bottom. 5 GridPanel A Grid panel provides a flexible area which consists of rows and columns. In a Grid, child elements can be arranged in a tabular form. Print Page Previous Next Advertisements ”;
XAML – Controls
XAML – Controls ”; Previous Next The XAML User Interface framework offers an extensive library of controls that supports UI development for Windows. Some of them have a visual representation such Button, Textbox, TextBlock, etc.; while other controls are used as containers for other controls or content, for example, images. All the XAML controls are inherited from System.Windows.Controls.Control. The complete inheritance hierarchy of controls is as follows − Here is the list of controls which we will discuss one by one in this chapter. Sr.No. Controls & Description 1 Button A control that responds to user input. 2 Calendar Represents a control that enables a user to select a date by using a visual calendar display. 3 CheckBox A control that a user can select or clear. 4 ComboBox A drop-down list of items a user can select from. 5 ContextMenu Gets or sets the context menu element that should appear whenever the context menu is requested through a user interface (UI) from within this element. 6 DataGrid Represents a control that displays data in a customizable grid. 7 DatePicker A control that lets a user select a date. 8 Dialogs An application may also display additional windows to the user to gather or display important information. 9 GridView A control that presents a collection of items in rows and columns that can scroll horizontally. 10 Image A control that presents an image. 11 ListBox A control that presents an inline list of items that the user can select from. 12 Menus Represents a Windows menu control that enables you to hierarchically organize elements associated with commands and event handlers. 13 PasswordBox A control for entering passwords. 14 Popup Displays content on top of existing content, within the bounds of the application window. 15 ProgressBar A control that indicates progress by displaying a bar. 16 ProgressRing A control that indicates indeterminate progress by displaying a ring. 17 RadioButton A control that allows a user to select a single option from a group of options. 18 RichEditBox A control that lets a user edit rich text documents with content like formatted text, hyperlinks, and images. 19 ScrollViewer A container control that lets the user pan and zoom its content. 20 SearchBox A control that lets a user enter search queries. 21 Slider A control that lets the user select from a range of values by moving a Thumb control along a track. 22 TextBlock A control that displays text. 23 TimePicker A control that lets a user set a time value. 24 ToggleButton A button that can be toggled between 2 states. 25 ToolTip A pop-up window that displays information for an element. 26 Window The root window which provides minimize/maximize option, Title bar, border and close button. In this chapter we will discuss all these controls with implementation. Print Page Previous Next Advertisements ”;
XAML – Event Handling
XAML – Event Handling ”; Previous Next The general concept of events in XAML is similar to events in other popular programming languages such as .NET and C++. In XAML, all of the controls expose some events so that they can be subscribed for specific purposes. Whenever an event takes place, the application will be notified and the program can react to them, e.g., close buttons are used to close a dialog. There are many types of events that can be subscribed for different behaviors of an application based on the requirement of that application, but the most commonly used events are those which are related to mouse and keyboard such as, Click MouseDown MouseEnter MouseLeave MouseUp KeyDown KeyUp In this chapter, we will use some of the basic and most commonly used events to understand how an event of a specific control can be linked to the code behind where the behavior will be implemented depending on what the user wants to do when a specific event occurs. Let’s have a look at a simple example of a button click event. Given below is the XAML implementation for Button control which is created and initialized with some properties and a Click event (Click=”OnClick”). <Window x:Class = “XAMLEventHandling.MainWindow” xmlns = “http://schemas.microsoft.com/winfx/2006/xaml/presentation” xmlns:x = “http://schemas.microsoft.com/winfx/2006/xaml” Title = “MainWindow” Height = “350” Width = “604”> <Grid> <Button x:Name = “button1” Content = “Click” Click = “OnClick” Width = “150” Height = “30” HorizontalAlignment = “Center” /> </Grid> </Window> Whenever this button is clicked, it will fire an OnClick event and you can add any type of behavior as a response to the Click. Let’s have a look at the OnClick event implementation which will show a message when this button is clicked. using System; using System.Windows; using System.Windows.Controls; namespace XAMLEventHandling { /// <summary> /// Interaction logic for MainWindow.xaml /// </summary> public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); } private void OnClick(object sender, RoutedEventArgs e) { MessageBox.Show(“Button is clicked!”); } } } When you compile and execute the above code, it will produce the following output − When you click on the button, the click (OnClick) event will be fired and the following message will be displayed. Now let’s have a look at a little bit complex example where multiple events are handled. Example The following example contains a textbox with ContextMenu which manipulates the text within the textbox. The following XAML code creates a TextBox, a ContextMenu, and MenuItems with some properties and events such as Checked, Unchecked, and Click. <Window x:Class = “XAMLContextMenu.MainWindow” xmlns = “http://schemas.microsoft.com/winfx/2006/xaml/presentation” xmlns:x = “http://schemas.microsoft.com/winfx/2006/xaml” Title = “MainWindow” Height = “350” Width = “604”> <Grid> <TextBox Name = “textBox1” TextWrapping = “Wrap” Margin = “10” Grid.Row = “7”> Hi, this is XAML tutorial. <TextBox.ContextMenu> <ContextMenu> <MenuItem Header = “_Bold” IsCheckable = “True” Checked = “Bold_Checked” Unchecked = “Bold_Unchecked” /> <MenuItem Header = “_Italic” IsCheckable = “True” Checked = “Italic_Checked” Unchecked = “Italic_Unchecked” /> <Separator /> <MenuItem Header = “Increase Font Size” Click = “IncreaseFont_Click” /> <MenuItem Header = “_Decrease Font Size” Click = “DecreaseFont_Click” /> </ContextMenu> </TextBox.ContextMenu> </TextBox> </Grid> </Window> Here is the implementation in C# for the different events which will be fired whenever a menu item is checked, unchecked, or clicked. using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using System.Windows.Data; namespace XAMLContextMenu { /// <summary> /// Interaction logic for MainWindow.xaml /// </summary> public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); } private void Bold_Checked(object sender, RoutedEventArgs e) { textBox1.FontWeight = FontWeights.Bold; } private void Bold_Unchecked(object sender, RoutedEventArgs e) { textBox1.FontWeight = FontWeights.Normal; } private void Italic_Checked(object sender, RoutedEventArgs e) { textBox1.FontStyle = FontStyles.Italic; } private void Italic_Unchecked(object sender, RoutedEventArgs e) { textBox1.FontStyle = FontStyles.Normal; } private void IncreaseFont_Click(object sender, RoutedEventArgs e) { if (textBox1.FontSize < 18) { textBox1.FontSize += 2; } } private void DecreaseFont_Click(object sender, RoutedEventArgs e) { if (textBox1.FontSize > 10) { textBox1.FontSize -= 2; } } } } When you compile and execute the above code, it will produce the following output − We recommend you to execute the above example code and experiment with some other events. Events Here are the commonly used events for different controls. Sr.No. Controls & Description 1 Checked Fires when a ToggleButton is checked. (Inherited from ToggleButton) 2 Click Occurs when a button control is clicked. (Inherited from ButtonBase) 3 ContextMenuClosing Occurs just before any context menu on the element is closed. (Inherited from FrameworkElement.) 4 ContextMenuOpening Occurs when any context menu on the element is opened. (Inherited from FrameworkElement.) 5 DataContextChanged Occurs when the value of the FrameworkElement.DataContext property changes. (Inherited from FrameworkElement) 6 DragEnter Occurs when the input system reports an underlying drag event with this element as the target. (Inherited from UIElement). 7 DragLeave Occurs when the input system reports an underlying drag event with this element as the origin. (Inherited from UIElement) 8 DragOver Occurs when the input system reports an underlying drag event with this element as the potential drop target. (Inherited from UIElement) 9 DragStarting Occurs when a drag operation is initiated. (Inherited from UIElement) 10 DropCompleted Occurs when a drag-and-drop operation is ended. (Inherited from UIElement) 11 DropDownClosed Occurs when the drop-down portion of the ComboBox closes. 12 DropDownOpened Occurs when the drop-down portion of the ComboBox opens. 13 GotFocus Occurs when a UIElement receives focus. (Inherited from UIElement) 14 Holding Occurs when an otherwise unhandled Hold interaction occurs over the hit test area of this element. (Inherited from UIElement) 15 Intermediate Fires when the state of a ToggleButton is switched to the indeterminate state. (Inherited from ToggleButton) 16 IsEnabledChanged Occurs when the IsEnabled property changes. (Inherited from Control) 17 KeyDown Occurs when a keyboard key is pressed while the UIElement has focus. (Inherited from UIElement) 18 KeyUp Occurs when a keyboard key is released while the UIElement has focus. (Inherited from UIElement) 19 LostFocus Occurs when a UIElement loses focus. (Inherited from UIElement) 20 ManipulationCompleted Occurs when a manipulation on
XAML – Dependency Properties
XAML – Dependency Properties ”; Previous Next A dependency property is a specific type of property where the value is followed by a keen property system which is also a part of the Windows Runtime App. A class which defines a dependency property must be inherited from the DependencyObject class. Many of the UI control classes which are used in XAML are derived from the DependencyObject class and support dependency properties. The following XAML code creates a button with some properties. <Window x:Class = “XAMLDependencyProperty.MainWindow” xmlns = “http://schemas.microsoft.com/winfx/2006/xaml/presentation” xmlns:x = “http://schemas.microsoft.com/winfx/2006/xaml” xmlns:local = “clr-namespace:XAMLDependencyProperty” Title = “MainWindow” Height = “350” Width = “604”> <Grid> <Button Height = “40” Width = “175” Margin = “10” Content = “Dependency Property”> <Button.Style> <Style TargetType = “{x:Type Button}”> <Style.Triggers> <Trigger Property = “IsMouseOver” Value = “True”> <Setter Property = “Foreground” Value = “Red” /> </Trigger> </Style.Triggers> </Style> </Button.Style> </Button> </Grid> </Window> The x:Type markup extension in XAML has a similar functionality like typeof() in C#. It is used when attributes are specified that take the type of the object such as <Style TargetType = “{x:Type Button}”> When you compile and execute the above code, it will produce the following MainWindow. When the mouse is over the button, it will change the foreground color of the button. When the mouse leaves the button, it will change back to its original color. The main difference between dependency properties and other CLR properties are − CLR properties can directly read/write from the private member of a class by using getter and setter. In case of dependency properties, it is not stored in a local object. Dependency properties are stored in a dictionary of key/value pairs which is provided by the DependencyObject class. It also saves a lot of memory because it stores the property when changed. It can be bound in XAML as well. In .NET framework, custom dependency properties can also be defined. Here are the steps to define custom dependency property in C#. Declare and register your dependency property with system call register. Provide the setter and getter for the property. Define a static handler to handle any changes that occur globally. Define an instance handler to handle any changes that occur to that particular instance. Given below is the code in C# for dependency property which defined to set the SetText property of the user control. using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; namespace WpfApplication3 { /// <summary> /// Interaction logic for UserControl1.xaml /// </summary> public partial class UserControl1 : UserControl { public UserControl1() { InitializeComponent(); } public static readonly DependencyProperty SetTextProperty = DependencyProperty.Register(“SetText”, typeof(string), typeof(UserControl1), new PropertyMetadata(“”, new PropertyChangedCallback(OnSetTextChanged))); public string SetText { get {return(string) GetValue(SetTextProperty); } set {SetValue(SetTextProperty, value);} } private static void OnSetTextChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { UserControl1 UserControl1Control = d as UserControl1; UserControl1Control.OnSetTextChanged(e); } private void OnSetTextChanged(DependencyPropertyChangedEventArgs e) { tbTest.Text = e.NewValue.ToString(); } } } Here is the XAML file in which the TextBlock is defined as a user control and the Text property will be assigned to it by the SetText dependency property. The following XAML code creates a user control with initializing its SetText dependency property and some other properties. <Window x:Class = “WpfApplication3.MainWindow” xmlns = “http://schemas.microsoft.com/winfx/2006/xaml/presentation” xmlns:x = “http://schemas.microsoft.com/winfx/2006/xaml” xmlns:views = “clr-namespace:WpfApplication3” Title = “MainWindow” Height = “350” Width = “604”> <Grid> <views:UserControl1 SetText = “Hellow World” /> </Grid> </Window> Let”s run this application and you can see immediately in our MainWindow that dependency property for user control has been successfully used as a Text. Print Page Previous Next Advertisements ”;
Writing XAML Application On MAC OS ”; Previous Next XAML applications can be developed on Mac as well. On Mac, XAML can be used as iOS and Android applications. To setup the environment on Mac, go to www.xamarin.com. Click on Products and select the Xamarin Platform. Download Xamarin Studio and install it. It will allow you to develop applications for the various platforms. XAML – C# Syntax In this chapter, you will learn the basic XAML syntax/rules to write XAML applications. Let’s have a look at a simple XAML file. <Window x:Class = “Resources.MainWindow” xmlns = “http://schemas.microsoft.com/winfx/2006/xaml/presentation” xmlns:x = “http://schemas.microsoft.com/winfx/2006/xaml” Title = “MainWindow” Height = “350” Width = “525”> <Grid> </Grid> </Window> As you can see in the above XAML file, there are different kinds of tags and elements. The following table briefly describes all the elements. Sr.No Elements & Description 1 <Window It is the opening object element or container of the root. 2 x:Class=”Resources.MainWindow” It is the partial class declaration which connects the markup to the partial class code behind defined in it. 3 xmlns Maps the default XAML namespace for WPF client/framework 4 xmlns:x XAML namespace for XAML language which maps it to x: prefix 5 > End of object element of the root. 6 <Grid> </Grid> Starting and closing tags of an empty grid object. 7 </Window> Closing the object element Syntax Rules for Object Element Syntax rules for XAML is almost similar to XML. If you take a look at an XAML document, then you will notice that actually it is a valid XML file. However, an XML file cannot be a valid XAML file. It is because in XML, the value of the attributes must be a string, while in XAML, it can be a different object which is known as Property element syntax. The syntax of an Object element starts with a left angle bracket (<) followed by the name of the object, e.g. Button. Define some Properties and attributes of that object element. The Object element must be closed by a forward slash (/) followed immediately by a right angle bracket (>). Example of simple object with no child element − <Button/> Example of object element with some attributes − <Button Content = “Click Me” Height = “30” Width = “60”/> Example of an alternate syntax to define properties (Property element syntax) − <Button> <Button.Content>Click Me</Button.Content> <Button.Height>30</Button.Height> <Button.Width>60</Button.Width> </Button> Example of Object with Child Element − StackPanel contains Textblock as child element <StackPanel Orientation = “Horizontal”> <TextBlock Text = “Hello”/> </StackPanel> Print Page Previous Next Advertisements ”;
XAML – Home
XAML Tutorial PDF Version Quick Guide Resources Job Search Discussion Welcome to the XAML tutorial for beginners. This tutorial puts greater emphasis on realtime implementation of the concept rather than discussing just the theory part. The primary objective of this tutorial is to provide you a better understating of what you can do with XAML development irrespective of the platform you are using. Audience This tutorial has been designed for all those readers who want to learn XAML and to apply it instantaneously in different type of applications. Prerequisites Before proceeding with this tutorial, you should have a basic understanding of XML, Web Technologies and HTML. Print Page Previous Next Advertisements ”;