views:

146

answers:

1

I am trying to create a static property, and the only way I could find to do it, is using DependencyProperty.

I have created the following fields in my class :

    public static readonly DependencyProperty UnreadMessagesProperty = DependencyProperty.Register("UnreadMessages", typeof(int), typeof(ErrorLog));

    public int UnreadMessages
    {
        get
        {
            return (int)GetValue(ErrorLog.UnreadMessagesProperty);
        }
        set
        {
            SetValue(ErrorLog.UnreadMessagesProperty, value);
        }
    }

And this compiles fine, but when I try to set the initial value of UnreadMessages in the contructor to 0 using the following code:

public ErrorLog()
{
    this.UnreadMessages = 0;
}

I get the following exception

Exception has been thrown by the target of an invocation.  
Error at object 'System.Windows.Data.Binding' in markup file 'Dashboard;component/main.xaml'.

And the Inner Exception message is below:

Value cannot be null.    
Parameter name: dp

And the Full Stack Trace of the inner exception:

   at System.Windows.DependencyObject.SetupPropertyChange(DependencyProperty dp)
   at System.Windows.DependencyObject.SetValue(DependencyProperty dp, Object value)
   at Dashboard.ErrorLog.set_UnreadMessages(Int32 value) in ErrorLog.xaml.cs:line 67
   at Dashboard.ErrorLog..ctor() in ErrorLog.xaml.cs:line 97
   at Dashboard.ErrorLog..cctor() in ErrorLog.xaml.cs:line 33

Singleton Instance Approach code:

public static readonly ErrorLog Instance = new ErrorLog();

What I am trying to do, is have a static UnreadMessages property which I can bind to in my Main class so I can display if there are any unread error messages that the user needs to be made aware of.

I tried using a singleton approach but I cannot seem to be able to bind to ErrorLog.Instance.UnreadMessages.

Could anyone help me out?

+1  A: 

I could reproduce the problem. In your ErrorLog class, I guess you have:

public static readonly ErrorLog Instance = new ErrorLog();
public static readonly DependencyProperty UnreadMessagesProperty = DependencyProperty.Register("UnreadMessages", typeof(int), typeof(ErrorLog));

Change it to:

public static readonly DependencyProperty UnreadMessagesProperty = DependencyProperty.Register("UnreadMessages", typeof(int), typeof(ErrorLog));
public static readonly ErrorLog Instance = new ErrorLog();

The order is simply reversed. Both elements are static and reference the same types, so .NET cannot order it. In the first case, the instance is created and the DP is used before being instantiated.

You could avoid this problem by specifying a default value when creating the DP.

On a 2nd note, the XAML is wrong. {x: Static } expects a type name as first parameter, not an instance. And UnreadMessages is not static, the DP is. Do not use DPs to create static properties.

If you need a static property, just declare one in your code-behind/data context class and bind it with {x: Static}.

Timores
Thanks a million! Didnt think this would make a difference!
LnDCobra
By the way, regarding your 2nd note. When I create a static property, I cannot implement NotifyPropertyChanged which ends up in my UI not being updated as this property changed. Any suggestion
LnDCobra
I was afraid you'd ask this :-)Have a look at: http://social.msdn.microsoft.com/forums/en-US/wpf/thread/b42027ab-07be-47a1-889a-a930d19fc4a5/ the last entry is an interesting solution, if it suits your needs.
Timores