WPF: Binding to static property of static class

You don’t need to use `static` properties… you can declare a class using the `Singleton` pattern, so there can only be one instance, just *like* a `static` class. Just use normal `public` CLR properties in this class… something like this (but with properties):

    public class StateManager : INotifyPropertyChanged
        private static StateManager instance = new StateManager();
        public StateManager() { }

        public StateManager Instance
            get { return instance; }


Then only reference it from a base view model using the `Instance` property like this:

    private StateManager stateManager = new StateManager();

    public StateManager StateManager
        get { return stateManager.Instance; }

Then you can access the properties in the UI simply, like this::

    <ribbon:ribboncheckbox Grid.Row="1" Label="Audit fields" 
        IsChecked="{Binding StateManager.AreAuditFieldsVisible}" ...></ribbon:ribboncheckbox>
    <ribbon:ribboncheckbox Grid.Row="2" Label="ISRCs on results" 
        IsChecked="{Binding StateManager.AreIsrcsVisibleOnSearchResults}" ...></ribbon:ribboncheckbox>

Leave a Reply