开发者

Data binding a string variable to textBlock in Phone 7?

I am writing this in XAML

<TextBlock  Text="{Binding Path=Test}" Grid.Column="1" Margin="52,46,32,50" Name="textBlockCheck"  />

and in CS

public partial class MainPage : PhoneApplicationPage
{
    private string _test = "SHUAIB";

    public string Test
    {
        get
        {
            return this._test;
        }
        set
        {
            if (value != t开发者_运维知识库his._test)
            {
                this._test = value;
            }
        }
    }//other code here}

But the problem is that nothing is being displayed in the textBlock:(. I know it will be very simple but I am very new to phone 7 and silverlight. I will be thankful to you:)


Set the DataContext of your main page to itself. Put this in the constructor of MainPage:

DataContext = this;


Since you don't specify a source in your binding, it will use the object defined in the DataContext property (which is generally inherited down. Looks like you want to bind to the containing page, so you could use something like Text="{Binding Path=Test, ElementName=myPage}" if your MainPage has the x:Name="myPage" attribute in XAML.

Or you can follow Daniel's suggestion and set DataContext to this, but this may be "overridden" if your TextBlock is defined in a DataTemplate.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜