Search code examples
c#silverlightwindows-phone-7data-bindingsilverlight-toolkit

Data-binding to singleton source works only on Windows Phone emulator, but not on device


I've created a singleton class to store information I want to share globally between controls in a Windows Phone 7 app I'm working on.

Specifically, I'm using a data-binding to sync the IsExpanded property between various Silverlight Toolkit ExpanderViews. The problem I'm experiencing is that the value doesn't seem to propagate, but only on a physical Windows Phone device...the app works fine on the emulator.

Since all other bindings to sources other than the singleton class in this project are working fine, I've assumed I implemented the binding and/or singleton incorrectly, or am missing something obvious...but every thread on this forum and others I've checked hasn't helped me solve this issue.

The singleton class is as follows:

class ControlStateContainer : INotifyPropertyChanged
{
    private static readonly ControlStateContainer _instance = new ControlStateContainer();
    private bool _optionsExpanded = false;

    private ControlStateContainer()
    { }

    public static ControlStateContainer Instance
    {
        get { return _instance; }
    }

    public bool OptionsExpanded
    {
        get { return _optionsExpanded; }
        set
        {
            _optionsExpanded = value;
            this.NotifyPropertyChanged("OptionsExpanded");
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;

    public void NotifyPropertyChanged(string name)
    {
        var handler = PropertyChanged;

        if (handler != null)
            PropertyChanged(this, new PropertyChangedEventArgs(name));
    }
}

And I'm binding the IsExpanded property of the ExpanderViews with the following code:

Binding _isExpandedBinding = new Binding
{
    Source = ControlStateContainer.Instance,
    Path = new PropertyPath("OptionsExpanded"),
    Mode = BindingMode.TwoWay
};

expander.SetBinding(ExpanderView.IsExpandedProperty, _isExpandedBinding);

The ExpanderViews behave as expected on the emulator, but when I deploy the app to a device the binding no longer seems to work.

I'm still quite new to C# and Windows Phone development in general and fully expect this to be a cringeworthily simple detail I've missed...any ideas?


Solution

  • Apparently the singleton class has to be explicitly declared public...now it works on both the emulator and the device.