Search code examples
.netwpfxaml

What does "{x:Static}" mean in XAML?


What does {x:Static} mean in XAML?

Code sample:

<SolidColorBrush Color="{x:Static SystemColors.ControlColor}" />

Solution

  • It is a way to insert any static value into XAML. For example, if I have a class:

    namespace A 
    { 
        public class MyConstants 
        {
            public static readonly string SomeConstantString = "BAM!";
        }
    }
    

    I can place it into a WPF UI using XAML like this:

    <TextBlock Text="{x:Static A:MyConstants.SomeConstantString}" />
    

    Notice, you will have to import the namespace in which MyConstants is defined into your XAML. So in the or element do something like:

    xmlns:A="clr-namespace:A"