Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Binding visibility to static property

Tags:

c#

binding

wpf

I have a control that has a label on it, that I would like to hide or show based on a global menu item for all instances of my control. If I click the button to hide labels, I want to to hide all of them.

My xaml looks like this:

<TextBlock Name="_label" Visibility="{Binding LabelShown}" VerticalAlignment="Center" HorizontalAlignment="Center"/>

in my code behind I have a property:

    private static Visibility _labelShown;
    public static Visibility LabelShown
    {
        get { return _labelShown; }
        set { _labelShown = value; }
    }

And I set DataContext = this;

When I change the static property, nothing happens. I assume this is because no controls are getting a property changed notification. I cannot implement INotifyPropertyChanged on it, because I cannot reference the non static property changed handler from my static property.

I feel like maybe this isn't the best way to do this, but I would really like to have one button (many levels above my actual control) drive the visibility for all instances.

like image 707
captncraig Avatar asked Feb 20 '26 07:02

captncraig


1 Answers

CodeNaked's solution works, but it uses a Singleton which has downsides when doing unit-testing. I prefer to approach global access problems by just having one settings instance at the application root, i.e. the App-class.

e.g.

public partial class App : Application
{
    private static Settings _settings = new Settings();
    public static Settings Settings
    {
        get { return _settings; }
    }

        ...

Where this property contains all the settings for the application. Binding then looks like this:

"{Binding Source={x:Static local:App.Settings}, Path=LabelsShown}"

Edit: If you are worried about dependencies you could also inject a reference to those settings in the constructor of any class where you need it, using its minimal interface.

e.g.

public class MyControl : ContentControl
{
    public interface IMyControlSettings
    {
        public bool LabelsShown { get; set; }
    }

    private IMyControlSettings _settings;

    public MyControl(IMyControlSettings settings)
    {
        _settings = settings;
        DataContext = _settings; // For easy binding, in most cases you probably do not want to do that since it prevents DataContext inheritance.
    }
}
public class Settings : Test.MyControl.IMyControlSettings, INotifyPropertyChanged
{
    public bool LabelsShown { get; set; }
    ...
}
like image 70
H.B. Avatar answered Feb 21 '26 22:02

H.B.



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!