100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace
.NET

x:Static and x:Type

Bridging XAML to CLR code: pulling in static field/property values with x:Static and referencing types themselves with x:Type.

Markup ExtensionsIntermediate8 min readJul 10, 2026
Analogies

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.

xml
<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

Was this page helpful?

Topics covered

#NET#XAMLStudyNotes#MicrosoftTechnologies#XStaticAndXType#Static#Type#Pulling#CLR#StudyNotes#SkillVeris