【问题标题】:How to acces text of textblock inside a dynamic stackpanel in wp8?如何在 wp8 的动态堆栈面板中访问文本块的文本?
【发布时间】:2014-09-30 22:36:23
【问题描述】:

我有一个包含堆栈面板的数据绑定列表框,其中包含许多文本框,并且文本正在从服务器动态加载。这就是 XAML 的样子-

<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>

我有一个事件“StackPanel_Tap”,我想访问“Name_Text”(TextBox) 中的文本以获取正在点击的堆栈面板元素。我想访问下面的方法存根中的文本...

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

【问题讨论】:

  • 你不能用Name_Text.Text吗?
  • 不,我不能使用它,因为它存在于绑定的文本框中..

标签: c# xaml windows-phone-8 data-binding listbox


【解决方案1】:

另一种方法是:

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

【讨论】:

    【解决方案2】:

    试试这个:

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

    【讨论】:

    • 谢谢!!像魅力一样工作!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-22
    • 1970-01-01
    • 2011-06-20
    • 2010-12-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多