![](/img/trans.png)
[英]c# windows app listbox inside listbox binding issue(universal app phone+pc)
[英]Binding textbox inside listbox in windows phone 7 C#
我在列表框數據模板中有tetbox。 該文本框是可編輯的。 如何在選擇更改事件中獲取文本框文本值。 如果我嘗試使用可視樹查找文本,則不會返回任何內容。 請幫忙
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:
<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>
我只是將文本框文本屬性模式添加為雙向。 在選擇更改事件中,將所選項目作為類。 var context =(lstProds.SelectedItem as ModelClass); 字符串a = context.Quantity;
<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>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.