首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在windows phone 7 C#中绑定列表框内的文本框

在windows phone 7 C#中绑定列表框内的文本框
EN

Stack Overflow用户
提问于 2014-07-14 15:00:31
回答 1查看 181关注 0票数 1

我在列表框的数据模板中有了tetbox。该文本框是可编辑的。如何在selection changed事件中获取textbox文本值。如果我尝试使用visual tree查找文本,它不会返回任何内容。请帮帮忙

代码语言:javascript
运行
复制
ListBoxItem item = this.lstProds.SelectedItem as ListBoxItem;
PhoneTextBox targetBox = FindFirstElementInVisualTree<PhoneTextBox>(item);
string a = targetBox.Text;

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

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

XAML:

代码语言:javascript
运行
复制
  <ListBox Name="lstProds" Margin="0,59,0,0" Background="{x:Null}" Foreground="Black" SelectionChanged="lstProds_SelectionChanged">
     <ListBox.ItemTemplate>
        <DataTemplate>
          <Grid Height="auto">
            <Border BorderBrush="Black" BorderThickness="1" Margin="68,63,-58,-13">
             <toolkit:PhoneTextBox Name="txtQuantity" Hint="Quantity"  Width="180" Text="{Binding Quantity}"></toolkit:PhoneTextBox>
            </Border>
           </Grid>
          </DataTemplate>
         </ListBox.ItemTemplate>
   </ListBox>
EN

回答 1

Stack Overflow用户

发布于 2014-07-14 20:55:39

我只是添加了textbox文本属性模式作为两种方式。在selection changed事件中,将所选项目作为类获取。var context = (lstProds.SelectedItem as ModelClass);string a= context.Quantity;

代码语言:javascript
运行
复制
  <ListBox Name="lstProds" Margin="0,59,0,0" Background="{x:Null}" Foreground="Black" SelectionChanged="lstProds_SelectionChanged">
            <ListBox.ItemTemplate>
                <DataTemplate>
<Grid>
<Border BorderBrush="Black" BorderThickness="1" Margin="91,69,0,9" HorizontalAlignment="Left" Width="145">
                        <toolkit:PhoneTextBox Name="txtQuantity" Hint="Quantity" Width="144" Text="{Binding Quantity,Mode=TwoWay}"></toolkit:PhoneTextBox>
                </Border>    
</Grid>
            </Border>
            </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/24731109

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档