I have a strange problem where a XAML control is not visible in the code behind. Here is a sample of the XAML:
<ListView Name="lvtest" Grid.Row="2" Grid.ColumnSpan="2" Margin="0,20,0,0"
ItemsSource="{Binding Content}" >
<ListView.ItemTemplate>
<DataTemplate>
<StackPanel>
<WebView Name="contentView" Style="{StaticResource BodyTextStyle}" />
<TextBlock Name="testtxt" Text="{Binding}" Style="{StaticResource BodyTextStyle}" Foreground="GreenYellow"/>
In the code behind:
this.lvtest is recognised, but:
this.contentView and this.testtxt are both not.
I’ve also tried x:Name.
I’m clearly missing something obvious here, I just can’t see what.
EDIT:
To clarify, the textbox control was going to be used to display some formatted text based on the binding, but what I have discovered is that the text is HTML formatted (this suggests using a WebView control). As far as I can see I need to NavigateToString to use the WebView control, and so can’t bind it as such.
What you’re missing is that the elements defined in a DataTemplate may be on the page any number of times, which can even change while running. Which one should be linked to the
contentViewfield? There’s no good way to answer that, so it simply doesn’t create acontentViewfield.To put it another way, you’re not defining that the page includes those elements, you’re declaring a template from which .NET can create elements.
If you let us know how you’re trying to use it, we might be able to suggest another way.
Edit: Something like this might work for you:
Then in the code: