Binding to static class property [duplicate] Binding to static class property [duplicate] wpf wpf

Binding to static class property [duplicate]


You can bind to ANY property on a static class using the x:Static markup extension but if thy do not implement any change tracking, it might cause errors on the refresh!

<TextBlock Text="{Binding Source={x:Static sys:Environment.MachineName}}" />


For those who use nested static classes to organize/seperate constants. If you need to Bind into nested static classes, It seems you need to use a plus (+) operator instead of the dot (.) operator to access the nested class:

{Binding Source={x:Static namespace:StaticClass+NestedStaticClasses.StaticVar}}

Example:

public static class StaticClass    {        public static class NestedStaticClasses        {            public static readonly int StaticVar= 0;        }    }


This has worked for me:

Text="{Binding Source={x:Static MyNamespace:MyStaticClass.MyProperty}, Mode=OneWay}"

Without Mode=OneWay I got an exception.