![](/img/trans.png)
[英]How to access textbox control located in DataTemplate tags of <phone:PanoramaItem.HeaderTemplate> from code - behind (MainPage.xaml.cs)
[英]how to access control located in DataTemplate from a control located outside DataTemplate tag
情況 :我在<phone:Panorama.TitleTemplate>
標簽的DataTemplate中有一個文本框控件
<phone:Panorama.TitleTemplate>
<DataTemplate>
<TextBlock Text="select your problem" Margin="7,40,0,0"
FontSize="60" Foreground="{StaticResource PhoneForegroundBrush}"/>
</DataTemplate>
</phone:Panorama.TitleTemplate>
現在我有另一個按鈕位於DataTemplate標簽外部和LayoutRoot Grid標簽內部。 此按鈕有一個click事件,其定義在cs文件后面的代碼中提供。
問題:我想訪問此按鈕的事件處理程序中的文本框。 我該怎么做 ?
您可以使用VisualTreeHelper
。
試試這個用於列表框的代碼段,您可以對其進行修改:
public string option_selected = "";
public int check_count = 0;
public void SearchElement(DependencyObject targeted_control)
{
var count = VisualTreeHelper.GetChildrenCount(targeted_control); // targeted_control is the listbox
if (count > 0)
{
for (int i = 0; i < count; i++)
{
var child = VisualTreeHelper.GetChild(targeted_control, i);
if (child is TextBlock) // specific/child control
{
TextBlock targeted_element = (TextBlock)child;
if (targeted_element.IsChecked == true)
{
if (targeted_element.Tag!= null)
{
option_selected = targeted_element.Tag.ToString();
}
return;
}
}
else
{
SearchElement(child);
}
}
}
else
{
return;
}
}
這將是一個很好的示例,您可以通過如何使用DataTemplate訪問列表框中的特定項目?
希望能幫助到你!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.