Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I bind a xaml property to a static variable in another class?

Tags:

c#

wpf

xaml

I have this xaml file in which I try to bind a Text-block Background to a static variable in another class, how can I achieve this ?

I know this might be silly but I just moved from Win-forms and feeling a little bit lost.

here is what I mean:

<TextBlock Text="some text"            TextWrapping="WrapWithOverflow"            Background="{Binding Path=SomeVariable}" /> 
like image 618
a7madx7 Avatar asked Apr 06 '13 18:04

a7madx7


People also ask

How can we access static variable from one class to another?

Static variables can be accessed by calling with the class name ClassName. VariableName. When declaring class variables as public static final, then variable names (constants) are all in upper case. If the static variables are not public and final, the naming syntax is the same as instance and local variables.

Can static variable be assigned another variable?

In Java, non-static final variables can be assigned a value either in constructor or with the declaration. But, static final variables cannot be assigned value in constructor; they must be assigned a value with their declaration.

Are static variables shared between instances?

Static variables are shared among all instances of a class. Non static variables are specific to that instance of a class. Static variable is like a global variable and is available to all methods. Non static variable is like a local variable and they can be accessed through only instance of a class.


1 Answers

First of all you can't bind to variable. You can bind only to properties from XAML. For binding to static property you can do in this way (say you want to bind Text property of TextBlock) -

<TextBlock Text="{Binding Source={x:Static local:YourClassName.PropertyName}}"/> 

where local is namespace where your class resides which you need to declare above in xaml file like this -

xmlns:local="clr-namespace:YourNameSpace" 
like image 152
Rohit Vats Avatar answered Oct 02 '22 16:10

Rohit Vats