shoayb
shoayb

Reputation: 223

Data binding a string variable of static class to textBlock in Phone 7?

Here is the C# code

public static class Global
{
    public static string Temp 
    { 
        get 
        {
            return temp;
        }
        set
        {
            temp = value;
        }
    }

    public static string temp="100";

}

and here is the xaml code of MainPage

 <TextBlock Text="{Binding Path=Temp}" Grid.Column="1" Margin="34,47,32,49" Name="textBlockCheck" />

I have declared the datacontext in MainPage.cs like this in its constructor:

this.DataContext= Global.Temp;

But there is nothing being displayed there in the textBlock. Thanks in advance for help.

Upvotes: 3

Views: 14457

Answers (5)

Matt Lacey
Matt Lacey

Reputation: 65564

You can't bind to a static class as binding requires an object instance.

You can, however, bind to static properties of a class.
You could use the following technique if you changed Global to not be static but left all it's properties as static.

Assuming:

namespace StaticBinding
{
    public class MyStaticClass
    {
        private static string myStaticProperty = "my static text";

        public static string MyStaticProperty
        {
            get { return myStaticProperty; }
            set { myStaticProperty = value; }
        }
    }
}

Then, if you define the following application resource:

.. xmlns:myns="clr-namespace:StaticBinding"

<Application.Resources>
    <myns:MyStaticClass x:Key="MyStaticClassResource" />
</Application.Resources>

Then in your page you can simply do the following:

<TextBlock Text="{Binding Path=MyStaticProperty, 
                  Source={StaticResource MyStaticClassResource}}" />

This will even give you intellisense on the Path.

This allows you to bind to "global" static variables and still leave the datacontext free to just contain any model you wish to bind to.

Upvotes: 9

shoayb
shoayb

Reputation: 223

Thanks everybody for help the problem is solved. Actually you create a simple class L

public partial class App : Application
{


    //--------------My Class

    private static MyClass _class = null;

    public static MyClass _Class
    {
        get
        {              
            if (_class == null)
                _class = new MyClass();

            return _class;
        }
    }

and in the page where you want to bind do this in .cs file's class constructor

DataContext = App._Class;

and lastly in .xaml version of your file write this to bind

<TextBlock Text="{Binding value}" Name="textBlockCheck" />

where

value

in Binding is actually the Property of the Class MyClass. :)

Thanks everyone!

Upvotes: 0

Rover
Rover

Reputation: 2230

You can use: <TextBlock Text="{Binding Path=.}" /> or <TextBlock Text="{Binding}" />

Upvotes: 0

isxaker
isxaker

Reputation: 9446

I think, that

this.Text = Global.Temp

Upvotes: 0

Robert
Robert

Reputation: 3302

I think it is not supported to bind to static property

Try to make the class singleton and bind to instance

this could help:

http://social.msdn.microsoft.com/Forums/en-IE/wpf/thread/257a41be-8168-401c-a915-cdc44e195a3f

Upvotes: 0

Related Questions