Reputation: 2383
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
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