Windows phone 8 在listbox控件中查找TextBox的名称

Windows phone 8 在listbox控件中查找TextBox的名称,windows-phone-8,Windows Phone 8,我有一个列表框(如下),其中有2个文本框,来自数据库的数据在文本框中显示良好。但是,我希望能够将这些值保存到数据库中,因为最初文本框是空白的,直到用户在其中输入它们的名称 由于文本框位于listbox中,我想我需要查找文本框的名称,以便能够访问text=“name”以保存到数据库中 我试过使用可视化树助手,但无法使其工作。可视化树助手是正确的方法吗 <ListBox HorizontalAlignment="Left" Margin="0,20,0,0" x:Name="lst_u

我有一个列表框(如下),其中有2个文本框,来自数据库的数据在文本框中显示良好。但是,我希望能够将这些值保存到数据库中,因为最初文本框是空白的,直到用户在其中输入它们的名称

由于文本框位于listbox中,我想我需要查找文本框的名称,以便能够访问text=“name”以保存到数据库中

我试过使用可视化树助手,但无法使其工作。可视化树助手是正确的方法吗

    <ListBox HorizontalAlignment="Left" Margin="0,20,0,0" x:Name="lst_userProfiles" VerticalAlignment="Top" >
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <StackPanel Orientation="Vertical" x:Name="stk_container">
                        <!-- FIRST NAME-->
                        <TextBlock Text="First Name: " FontSize="22" />
                        <TextBox Text="{Binding UP_firstName, Mode=TwoWay}" Width="400" x:Name="txt_firstName" />
                        <!-- LAST NAME -->
                        <TextBlock Text="Surname: " FontSize="22" />
                        <TextBox Text="{Binding UP_lastName, Mode=TwoWay}" Width="400" x:Name="txt_surname" />
                    </StackPanel>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>

代码隐藏

     private void btn_save_Click(object sender, RoutedEventArgs e)
    {
        //first name
        ListBoxItem _firstName = this.lst_userProfiles.ItemContainerGenerator.ContainerFromItem("txt_firstName") as ListBoxItem;
        TextBox firstName = FindFirstElementInVisualTree<TextBox>(_firstName);
        string getFirstName = Convert.ToString(firstName);

        //lst name
        ListBoxItem _lasyName = this.lst_userProfiles.ItemContainerGenerator.ContainerFromItem("txt_surname") as ListBoxItem;
        TextBox lastName = FindFirstElementInVisualTree<TextBox>(_lasyName);
        string getLastName = Convert.ToString(lastName);



        Service1Client svc = new Service1Client();
        svc.AddProfileCompleted += new EventHandler<System.ComponentModel.AsyncCompletedEventArgs>(svc_AddProfileCompleted);
        svc.AddProfileAsync(loginID, getFirstName, getLastName);
    }

     private T FindFirstElementInVisualTree<T>(DependencyObject parentElement) where T : DependencyObject
    {
        var count = VisualTreeHelper.GetChildrenCount(parentElement);

        for (int i = 0; i < count; i++){
            var child = VisualTreeHelper.GetChild(parentElement, i);

            if (child != null && child is T) {
                return (T)child;
            }
            else {
                var result = FindFirstElementInVisualTree<T>(child);
                if (result != null)
                    return result;
            }
        }
        return null;
    }
private void btn\u保存\u单击(对象发送方,路由目标)
{
//名字
ListBoxItem _firstName=this.lst_userProfiles.ItemContainerGenerator.ContainerFromItem(“txt_firstName”)作为ListBoxItem;
TextBox firstName=findfirstelementvisualtree(_firstName);
字符串getFirstName=Convert.ToString(firstName);
//lst名称
ListBoxItem _lasyName=this.lst_userProfiles.ItemContainerGenerator.ContainerFromItem(“txt_姓氏”)作为ListBoxItem;
TextBox lastName=findfirstelementvisualtree(_lasyName);
字符串getLastName=Convert.ToString(lastName);
Service1Client svc=新Service1Client();
svc.AddProfileCompleted+=新事件处理程序(svc_AddProfileCompleted);
AddProfileAsync(loginID、getFirstName、getLastName);
}
私有T FindFirstElementVisualTree(DependencyObject parentElement),其中T:DependencyObject
{
var count=VisualTreeHelper.GetChildrenCount(parentElement);
for(int i=0;i
我真的不明白你为什么要这样做,但你可以试试这段代码

        List<string> name = new List<string>();
        int i = 0;
        foreach(ListPickerItem lpi in lst_userProfiles.Items )
        {
            foreach(TextBox tb in (lpi.Content as StackPanel).Children)
            {
                name[i++] = tb.Name;
            }
        }
List name=新列表();
int i=0;
foreach(lst_userProfiles.Items中的ListPickerItem lpi)
{
foreach(文本框tb in(lpi.Content as StackPanel).Children)
{
name[i++]=tb.name;
}
}

退一步,如果您的目标是获取文本框的值,那么迭代可视化树不是正确的方法。我相信你的问题已经有了答案:双向绑定。只需使用绑定到视图的列表来保存所需数据。您所说的“只需使用绑定到视图的列表来保存所需数据”是什么意思?您的文本框是双向绑定的(请参阅代码),这意味着当文本字段更改时,它将自动更新基础数据。更多信息:是的,我不确定我需要双向绑定。我正在通过服务客户端更新值。我需要将TextBox.Text传递到服务客户机的参数中。BrianV是正确的。您可能希望将列表框绑定到ObservableCollection。它让你一切都变得容易。