microsoft5 min read

XAML Tutorial: Learn UI Markup from Scratch (2026)

XAML Tutorial: Learn UI Markup from Scratch (2026)

Published:  |  Category: Microsoft  |  Reading time: ~15 min
XAML Tutorial: Learn UI Markup from Scratch (2026)

When I first saw XAML, I thought it was XML with extra steps. But the moment I realized that every element maps to a .NET object and every attribute maps to a property, the mental model clicked. XAML is not just a configuration file — it is a declarative language for constructing object graphs. I have used it across WPF, UWP, WinUI, and Xamarin.Forms, and while each platform has its specifics, the core XAML concepts remain consistent.

XAML (eXtensible Application Markup Language) is Microsoft's declarative markup language for instantiating .NET objects. It is the foundation of WPF, UWP, WinUI 3, .NET MAUI, and Silverlight. XAML separates UI definition from runtime logic, enables designer-developer collaboration, and provides a strongly-typed, compiled approach to UI construction. This tutorial covers the essential XAML patterns and concepts you need to work with any XAML-based framework.

XAML Syntax: Elements, Attributes, and Namespaces

XAML syntax is straightforward: XML elements map to .NET classes, XML attributes map to properties or events. The xmlns declarations import .NET namespaces as XML namespaces. The default WPF namespace brings in all WPF controls, while the x namespace provides language-level features like x:Class, x:Name, x:Key, and x:Type. XAML is case-sensitive — Button is valid, button is not — because it maps directly to CLR type names.

Property element syntax lets you set complex properties that cannot be expressed as simple strings. Instead of a single attribute, you use a child element named TypeName.PropertyName. Attached properties, like Grid.Row and Canvas.Left, are properties defined by one type but set on child elements. They are specified using the format DefiningType.PropertyName.


    
        
    

Resources, Styles, and Reusability

XAML resources are reusable objects defined in a ResourceDictionary. Any object — brushes, styles, templates, converters, animations — can be stored as a resource. Resources are referenced using StaticResource or DynamicResource markup extensions. StaticResource resolves at load time and is faster. DynamicResource resolves at runtime and responds to resource changes, useful for theme switching.

Styles are the primary mechanism for reusing property values across multiple elements. A Style contains Setter elements that assign values to dependency properties. Styles can inherit from other styles using the BasedOn property, enabling a hierarchy of base and derived styles. Resources cascade through the element tree — a resource defined at Window level is available to all children.


    
    
    


Data Binding with Markup Extensions

Data binding is the cornerstone of XAML-based UI development. The Binding markup extension connects a target property on a FrameworkElement to a source property on any object. The Path specifies the source property. Mode controls the direction — TwoWay for editable fields, OneWay for display-only. UpdateSourceTrigger controls when the source is updated.

Value converters implement IValueConverter to transform data between source and target types. A BooleanToVisibilityConverter converts true to Visible and false to Collapsed. Converters receive the culture parameter and can be parameterized with ConverterParameter. Multi-binding and PriorityBinding handle advanced scenarios where a single target depends on multiple sources.






    

Control Templates and DataTemplates

ControlTemplates completely redefine how a control renders while preserving its behavior. The Template property on Control accepts a ControlTemplate. Inside the template, the TemplatedParent refers to the control being styled, and TemplateBinding provides a shorthand for binding to TemplatedParent properties. Triggers within the template respond to property changes.

DataTemplates define how non-visual data objects are rendered. A DataTemplate assigned to ListBox.ItemTemplate specifies the visual tree for each data item. The DataType property can auto-apply a template based on the type of the bound object — if your collection contains Product and Customer types, separate DataTemplates render each type appropriately.


    
        
    
    
        
            
        
        
            
        
    



    
        
            
            
        
    

Attached Properties and Behaviors

Attached properties are a special type of dependency property defined by one class but set on instances of other classes. Grid.Row and Canvas.Left are the classic examples. The defining class provides static Get/Set methods. Attached properties can be used for any scenario where a parent element needs to store per-child data.

Behaviors extend the functionality of UI elements without subclassing. The Interaction.Behaviors attached property from the Microsoft.Xaml.Behaviors library lets you attach reusable behavior objects to any element. A drag behavior, a validation behavior, or a numeric-only input behavior can be written once and attached declaratively in XAML.

public class NumericOnlyBehavior : Behavior
{
    protected override void OnAttached()
    {
        AssociatedObject.PreviewTextInput += OnPreviewTextInput;
        DataObject.AddPastingHandler(AssociatedObject, OnPaste);
    }

    private void OnPreviewTextInput(object sender, TextCompositionEventArgs e)
    {
        e.Handled = !e.Text.All(char.IsDigit);
    }

    protected override void OnDetaching()
    {
        AssociatedObject.PreviewTextInput -= OnPreviewTextInput;
        DataObject.RemovePastingHandler(AssociatedObject, OnPaste);
    }
}

Animation, Storyboards, and Visual States

XAML animations change dependency property values over time. Storyboards coordinate multiple animations. A DoubleAnimation animates double properties like Opacity or Width, ColorAnimation animates colors. Animations can be triggered by events, property triggers, or VisualState changes. The Timeline class provides Duration, RepeatBehavior, AutoReverse, and BeginTime.

VisualStateManager (VSM) defines visual states for controls — Normal, Pressed, Disabled, Focused — and transitions between them. Each VisualState contains a Storyboard that sets property values or triggers animations. VSM is the modern approach to interactive UI, replacing the older Trigger-based approach in many scenarios.

Frequently Asked Questions

What is the difference between XAML and HTML?

XAML is a declarative language for constructing .NET object graphs, strongly typed and compiled. HTML is a markup language for document structure, interpreted by browsers. XAML elements map to CLR classes, while HTML elements have predefined meanings.

Can I use XAML on non-Windows platforms?

Yes. .NET MAUI uses XAML for cross-platform mobile and desktop apps. The core XAML concepts transfer, though some platform-specific controls differ.

What is the difference between StaticResource and DynamicResource?

StaticResource resolves the resource once at load time and is faster. DynamicResource watches for resource changes and updates the target at runtime, which is needed for theme switching.

How do I debug XAML binding errors?

Enable binding trace with PresentationTraceSources.TraceLevel=High on the Binding. Use tools like Snoop or Live Visual Tree in Visual Studio to inspect runtime values.

Originally published on Ayodhyyya. Last updated June 1, 2026.