开发者

Problem with dependency property

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.DependencyObje开发者_开发知识库ct.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?


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}.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜