Click here to Skip to main content
15,891,981 members
Please Sign up or sign in to vote.
4.50/5 (2 votes)
See more:
I am having a hard time binding the text I enter in a TextBox to a static property in a non-static class. Here are the specifics:

UI is in the main window, the textbox is names tb.
The class (named c) is non-static and public.
the property (named p) is static - as in public static string p { get; set;}
the required direction is from the UI to the property; The other direction is not required, but doesn't make a difference.
I have tried lots of examples of syntax, but cannot manage to do so.

Please help!
Posted
Updated 30-Jul-15 0:20am
v2

Not sure i understand you well (because of this: "the required direction is from the UI to the property; The other direction is not required, but doesn't make a difference"), but...

I'd suggest to read this: Static Classes and Static Class Members (C# Programming Guide)[^] - Static Members part:
MSDN wrote:
A non-static class can contain static methods, fields, properties, or events. The static member is callable on a class even when no instance of the class has been created. The static member is always accessed by the class name, not the instance name.


This might be helpful too: Static Constructors (C# Programming Guide)[^]

So, you have to re-think your class definition.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 30-Jul-15 10:23am    
Exactly. A 5.
—SA
Maciej Los 2-Aug-15 15:33pm    
Thank you Sergey!
 
Share this answer
 
Comments
Maciej Los 30-Jul-15 6:49am    
5ed!
You hit 10!
Sergey Alexandrovich Kryukov 30-Jul-15 10:24am    
5ed.
—SA
stibee 30-Jul-15 10:26am    
What means 5ed?
Sergey Alexandrovich Kryukov 30-Jul-15 10:41am    
Members vote by clicking on the stars above, from 1 (very bad), to 5 (excellent).
—SA
You can't bind to a static like that. There's no way for the binding to get notified of updates since there's no DependencyObject (or object instance that implement INotifyPropertyChanged) involved.
 
Share this answer
 
The following code will work:

C#
private static string _p;


 public static string P
 {
   get { return _p; }
   set
   {
     _p = value;
   }
 }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900