I'm working on an app for WP7 but i have this small problem:
<TextBlock Name="rName"
Text="{Binding LineOne2}"
TextWrapping="NoWrap"
Style="{StaticResource PhoneTextExtraLargeStyle}" />
I have this Textblock
binding to LineOne2
, I set up a tap event to launch another page when it gets tapped.
The problem is, when I try to get the value of that text rName.Text
, it shows that its unavailable.
So I was wandering is there any possible way to get the value of that TextBlock
to use it as parameter for a method in another page even if it was binding? Or is there any other way to do so?
My list contains 2 textboxes and an image in a stackpanel.... I need to press in that textbox open a new page with the value of that textbox in that page value and as a parameter to a method.
edit:
<ListBox ItemsSource="{Binding Items1}"
Margin="14,149,8,8"
Foreground="#FF7A0100"
Grid.RowSpan="2">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Name="featuredPanel"
Orientation="Horizontal"
Margin="0,0,0,17"
Height="105"
Width="432"
Tap="featuredPanel_Tap">
<!--Replace rectangle with image-->
<Image Height="100"
Width="100"
Source="{Binding ImagePath1}">
</Image>
<StackPanel Width="311">
<TextBlock x:Name="rName"
Text="{Binding LineOne1}"
TextWrapping="NoWrap"
Style="{StaticResource PhoneTextExtraLargeStyle}" />
<TextBlock Text="{Binding LineTwo1}"
TextWrapping="Wrap"
Margin="12,-6,12,0"
Style="{StaticResource PhoneTextSubtleStyle}" />
</StackPanel>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
Also I'm binding data from an XML file.
I need to use the TextBox "rName" to get the value and use it to do a linq query on the xml file to get the addressa and phone number from that XML file and bind them to the Textboxes in the other page.
The reason why you can't access the TextBlock
by name is because it is part of a ListBox
's ItemTemplate
.
Think about it, the ListBox
is going to have as many items as there are elements in the Items1
collection, so how can you specify which TextBlock
you're referring to if you're accessing it by name?
There are a couple of different ways to solve this problem. One way is to save the currently tapped item in the featuredPanel_Tap
handler. Add this line to the handler:
var currentItem = ((sender as StackPanel).DataContext) as MyItemType;
MyItemType
is the type of object that is contained in the Items1
collection. Then you can access the text as currentItem.LineOne1
or currentItem.LineTwo1
.
Another way is to create a TwoWay
binding for the ListBox
's SelectedItem
or SelectedIndex
property. The bound item would be automatically updated to the correct object within Items1
when the user taps on a ListBox
item.