Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 630913
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T19:54:27+00:00 2026-05-13T19:54:27+00:00

I am trying to create a static property, and the only way I could

  • 0

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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T19:54:27+00:00Added an answer on May 13, 2026 at 7:54 pm

    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
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to find why the class cant be created as a static?
I'm trying to access a static method in a Utils class I created: public
I'm trying to create a constant static collection of a custom class, like so:
I am trying to create an SQLite database. The create method: private static class
I am trying to create a menu bar for my application. I have created
I am trying to find a easy to extend way to create objects at
I am trying to create a class with a single property which can be
I have simple static lib in xcode with the only class test.h: @interface TestClass
I am trying to create a very simple UITableView with static cells from within
I'm trying to dynamically create a connection string during compile time: private static string

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.