x:Static: Pulling in a CLR Static Value
{x:Static} retrieves the value of a static field, static property, constant, or enum member from managed code and inserts it into XAML at parse time, using the form {x:Static prefix:TypeName.MemberName}. This is the bridge between compile-time CLR constants and the XAML object graph: instead of duplicating a value like Math.PI or a custom AppConstants.MaxRetryCount as a magic literal in markup, XAML can reference the single source of truth defined in code, and the value is baked in once, exactly like a StaticResource lookup, since static fields do not change identity at runtime.
Cricket analogy: It is like quoting a fixed law of cricket, such as the six-ball over rule from the MCC Laws of Cricket, directly rather than re-typing the number 6 everywhere and hoping it stays consistent, just as x:Static references a single static field instead of duplicating its value.
x:Type: Referencing a Type Object
{x:Type TypeName} produces a System.Type object representing the given CLR type, used anywhere XAML needs to reference a type rather than an instance, such as DataTrigger's Binding combined with RelativeSource AncestorType={x:Type Window}, a Style's TargetType, or a DataTemplate's DataType. In WPF this is the XAML-markup equivalent of C#'s typeof() operator; in some XAML dialects a bare TypeName string works for TargetType because the XAML parser applies an implicit type converter, but AncestorType and other properties typed as System.Type generally require the explicit {x:Type ...} syntax.
Cricket analogy: It is like naming a fielding position by role, 'the current gully fielder', rather than naming the specific player instance occupying it, just as {x:Type Window} refers to the Window type itself rather than any one window instance.
<Window x:Class="DemoApp.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:sys="clr-namespace:System;assembly=mscorlib"
xmlns:local="clr-namespace:DemoApp">
<StackPanel>
<!-- x:Static pulling in a CLR constant -->
<TextBlock Text="{x:Static local:AppConstants.MaxRetryCount}" />
<TextBlock Text="{x:Static sys:Math.PI}" />
<!-- x:Type used with RelativeSource to bind to an ancestor Window -->
<TextBlock Text="{Binding RelativeSource={RelativeSource
AncestorType={x:Type Window}}, Path=Title}" />
<!-- x:Type used as a Style's TargetType -->
<Style TargetType="{x:Type Button}">
<Setter Property="Padding" Value="8,4" />
</Style>
</StackPanel>
</Window>Because {x:Static} is resolved at parse time exactly like {StaticResource}, referencing a static field that the developer intends to change at runtime (e.g. via reflection) will not update the UI; x:Static is only appropriate for values that are genuinely constant for the lifetime of the application.
- {x:Static prefix:TypeName.MemberName} resolves a static field, property, constant, or enum member from CLR code into XAML.
- x:Static is the XAML equivalent of dereferencing a static member in C#, avoiding duplicated magic literals.
- {x:Type TypeName} produces a System.Type object, the XAML equivalent of C#'s typeof() operator.
- x:Type is used wherever XAML needs a type reference rather than an instance, e.g. Style.TargetType, DataTemplate.DataType, RelativeSource.AncestorType.
- Some properties typed as TargetType accept a bare type-name string via an implicit converter, but AncestorType generally requires explicit {x:Type ...}.
- Both extensions resolve once at parse time, so they are unsuitable for values that change at runtime.
- A clr-namespace XML namespace mapping (e.g. xmlns:sys="clr-namespace:System;assembly=mscorlib") must be declared before x:Static or x:Type can reference a type in that namespace.
Practice what you learned
1. What does {x:Static local:AppConstants.MaxRetryCount} do?
2. What C# operator is {x:Type TypeName} conceptually equivalent to?
3. Which XAML usage typically requires the explicit {x:Type ...} syntax rather than a bare type name?
4. Why is x:Static inappropriate for a value the developer intends to change at runtime via reflection?
5. What must be declared before x:Static or x:Type can reference a type in a custom CLR namespace?
Was this page helpful?
You May Also Like
Markup Extensions Overview
How XAML's curly-brace syntax defers value creation to parse time through classes that implement MarkupExtension.
StaticResource and DynamicResource
The difference between resolving a resource once at parse time versus keeping a live, updating reference to it.
The Binding Markup Extension
How {Binding} connects UI properties to data-source properties, including Path, Mode, and source-resolution options.
Related Reading
Related Study Notes in Microsoft Technologies
Browse all study notesWindows 10 / UWP Development Study Notes
.NET · 30 topics
Microsoft TechnologiesWindows Batch Scripting Study Notes
Batch · 30 topics
Microsoft TechnologiesMFC (Microsoft Foundation Classes) Study Notes
C++ · 30 topics
Microsoft TechnologiesSilverlight Study Notes
.NET · 30 topics
Microsoft TechnologiesWPF (Windows Presentation Foundation) Study Notes
.NET · 30 topics
Microsoft TechnologiesMVVM Design Pattern Study Notes
.NET · 30 topics