Khayam Gondal
Khayam Gondal

Reputation: 2383

Unable to access TextBox in WP7 code

i have my xaml as following

<Button>
    <Button.ContentTemplate>
        <DataTemplate>
            <TextBlock x:Name="firstBlock"/>
        </DataTemplate>
    </Button.ContentTemplate>
</Button>

problem is that i cant use TextBlock in code. it gives error

The name 'firstBlock' does not exist in the current context

Upvotes: 0

Views: 168

Answers (1)

Igor Ralic
Igor Ralic

Reputation: 15006

It's because you're using it as a part of DataTemplate.

Why not just use it like this:

<Button Name="btn">
    <Button.Content>
        <TextBlock x:Name="firstBlock"/>
    </Button.Content>
</Button>

Or if it's just the TextBlock you need in your button, just call

btn.Content = "some text";

Upvotes: 3

Related Questions