tags:

views:

1539

answers:

3

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

Code sample:

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

I found the answer at <http://stackoverflow.com/questions/32395/xaml-accessing-static-fields> (from the answer suggestions), which links to http://msdn.microsoft.com/en-us/library/ms742135.aspx but I figured this would still be useful as a question to have on the site.

Mark Cidade
Yeah, but a simple google search of "x:Static xaml" finds it too!
Andrew Rollings
At the time I thought "search StackOverflow" not "search Google" and since I got my answer and wanted to get back to coding, I didn't bother to do a cross-reference with Google. I had the code and it worked—I was just curious what as to what it meant.
Mark Cidade
That brings up a good point. I wonder if a critical mass will be reached where stackoverflow becomes the first point of call rather than google for the majority of developers.
Andrew Rollings
+4  A: 

From MSDN: http://msdn.microsoft.com/en-us/library/ms742135.aspx

References any static by-value code entity defined in a Common Language Specification (CLS) compliant way The property referenced is evaluated prior to loading the remainder of the XAML page and can be used to provide the value of a property in XAML.

TcKs
+12  A: 

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"
siz