简体   繁体   中英

How to acces text of textblock inside a dynamic stackpanel in wp8?

I have a data binded ListBox containing a stackpanel, which contains a number of textbox and the text is being dynamically loaded from server. This is how the XAML looks like-

<ListBox x:Name="SearchColl_List" ItemsSource="{Binding }" >
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <!--Main Stack-->

                    <StackPanel x:Name="Coll_Stack" Orientation="Vertical"   
                                Margin="10,0,6,20" 
                                Tap="StackPanel_Tap">
                                      <TextBlock x:Name="Name_Text"
                                        Text="{Binding Name}"
                                       FontSize="20"
                                       Width="450"    
                                           HorizontalAlignment="Left"
                                           Foreground="#33706b"
                                       TextWrapping="Wrap"
                                       />
                                    <TextBlock Text="{Binding Stream}"
                                            Width="450"

                                           HorizontalAlignment="Left"
                                       FontSize="20"
                                       Foreground="Green"
                                       TextWrapping="Wrap"
                                      />
                                </StackPanel>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>

I have an event "StackPanel_Tap" and I want to access the Text in "Name_Text"(TextBox) for the stack panel element that is being tapped. I want to access the text in following Method Stub...

private void StackPanel_Tap(object sender, System.Windows.Input.GestureEventArgs e)
    {
       //Access the Text in "Name_Text" for current element that is tapped
    }

尝试这个:

((sender as StackPanel).FindName("Name_Text") as TextBlock).Text

另一种方法是:

(sender as StackPanel).Children[0].Text

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM