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?