55

I want to bind a textblock text to a property of a static class. Whenever the property value of the static class changes, it should reflect to the textblock which is on the other window or custom control.

H.B.
  • 166,899
  • 29
  • 327
  • 400
Vinod Maurya
  • 4,167
  • 11
  • 50
  • 81

4 Answers4

86

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}}" />
rudigrobler
  • 17,045
  • 12
  • 60
  • 74
  • 1
    That is, the property setter needs to raise the PropertyChanged event, just like any bound properties, to refresh properly. – Alex Paven Oct 05 '10 at 10:46
  • 1
    you need to add "xmlns:sys="clr-namespace:System;assembly=mscorlib" to your tag in order to make the code snipplet work, otherwise the namespace "sys" is not defined – DonGru Aug 05 '16 at 14:04
  • @AlexPaven: When invoking the PropertyChanged event, what do we send as the `sender` parameter? null? – Fandi Susanto Aug 19 '17 at 08:15
  • In general for a static property? Sure I guess null would be appropriate; see https://stackoverflow.com/questions/41519386/static-property-using-inotifypropertychanged-c-sharp – Alex Paven Aug 20 '17 at 13:40
  • Even if static property notifications are implemented, `Source+x:Static` doesn't enable notifications, you have to use `Path`: `` – CodingNinja Aug 01 '23 at 06:16
24

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;

        }
    }
CodyF
  • 4,977
  • 3
  • 26
  • 38
20

This has worked for me:

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

Without Mode=OneWay I got an exception.

NoOne
  • 3,851
  • 1
  • 40
  • 47
  • This method technically works for me, with a strange error. When I use a snippet like this it says that my static class is not part of the namespace (which it is) which gives me `xaml` an "Invalid Markup" screen on the designer. The strange part is that when I run the program it does not error out. Not only that but the snippet that is showing the error is working perfect. any idea what may be causing this? – ARidder101 Jun 05 '17 at 18:18
  • 2
    @ARidder101 No idea. Sorry. It's been a while since I dealt with this. But it could be simply some bug in the intellisense. – NoOne Jun 17 '17 at 16:27
-1

It worked for me!

When you have static class with static property like this

 namespace App.Classes
 {
     public static class AppData
     {
         private static ConfigModel _configModel;
         public static ConfigModel Configuration
         {
            get { return _configModel; }
            set { _configModel = value; }
         }
     }

     public class ConfigModel : INotifyPropertyChanged
     {
         public event PropertyChangedEventHandler PropertyChanged;

          private bool _text = true;
          public bool Text
          {
               get { return _text ; }
               set { 
                     _text = value; 
                     PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("Text"));
               }
          }
      }
}

You can bind it to xaml like this.

xmlns:c="clr-namespace:App.Classes"

<TextBlock Text="{Binding Path=Text, Source={x:Static c:AppData.Configuration}}"/>
Masuri
  • 894
  • 2
  • 9
  • 19